Jump to content

Date based condition


Wendi
Go to solution Solved by Wendi,

Recommended Posts

I'd like to add a date based condition to the code I have to reset the counter on every Sunday 00:00:00 but I can't figure out how to do it.

This is what I have, but it doesn't reset the counter to 0.

//COUNT PAGEVIEWS
function wpb_set_post_views($postID) {
    $count_key = 'wpb_post_views_count';
    $count = get_post_meta($postID, $count_key, true);
    $today = date('l H:i:s');
    if($count=='' || $today == 'Sunday 00:00:00') {
        $count = 0;
        delete_post_meta($postID, $count_key);
        add_post_meta($postID, $count_key, '0');
    }else{
        $count++;
        update_post_meta($postID, $count_key, $count);
    }
}

Could someone help me with this?

 

Thanks

Link to comment
Share on other sites

This will only trigger if the time is exactly sunday at 00:00:00, which is pretty specific.

 

Why do you do this anyway?

 

I want to count the pageviews and based on the number of pageviews I want to order the posts on my homepage. That part works already.

But what I also want is to reset the counter once a week to start all over again. In this way I'll have the posts on my homepage where people are most interested in that week.

Link to comment
Share on other sites

 

You can not compare date and string using ==

 

Try casting $today to string

if($count=='' || (string) $today === 'Sunday 00:00:00')

 

The "date" $today is a string.

 

$today = date('l H:i:s');

 

 

 

date():

Returns a string formatted according to the given format string using the given integer timestamp or the current time if no timestamp is given.

 

@Wendi,

What you apparently are trying to achieve is to reset the value of $count the first time this function is run each week. So, if it is run on Sunday morning it is reset to 0. Then each instance of the function running will increment the value by 1 - until the first execution the next Sunday. So, you need to determine how to know when it is the first run of the function each week. Let's say the function is run for the first time at 1:00am and the value is reset to 0. But, then if it is executed again at 2:00am how do you know that the value was previously reset at 1:00am? In fact, you can't guarantee that the function would get executed every Sunday, so you really want to know the first time the function is executed each week. You will need to store another value somewhere to track this.

 

I would set a value for "the next reset timestamp" which on midnight at the beginning of the next Sunday. Then on each execution of the function, check if the current timestamp is greater than the next reset timestamp. If yes, reset the $count value and create a new reset timestamp.

//COUNT PAGEVIEWS
function wpb_set_post_views($postID)
{
    $count_key = 'wpb_post_views_count';
    $count = get_post_meta($postID, $count_key, true);
    $nextResetTimestamp = resetTimestamp();
    if($count=='' || time() > $nextResetTimestamp)
    {
        $count = 0;
        delete_post_meta($postID, $count_key);
        add_post_meta($postID, $count_key, '0');
        resetTimestamp(strtotime('next sunday'));
 
    }else{
        $count++;
        update_post_meta($postID, $count_key, $count);
    }
}

For brevity, the above assumes a function exists called reset Timestamp(). If no parameter is passed the function would return the next reset timestamp that was saved. Else, if a parameter is passed, the function would use that value to change the next reset timestamp.

 

But, you could do this much easier using a database by just tracking ALL hits and then doing a SELECT query only on the time period you want to report on.

Edited by Psycho
Link to comment
Share on other sites

Eiseth solution works, but what DaveyK says is also true. When no one goes to the webpage at exactly that time there is no counter reset.

 

Maybe a better question is: how to reset the counter ON a specific day and time.

Link to comment
Share on other sites

  • Solution

I managed with a different approach, because I was stuck for the above reasons.

 

I put the delete and add lines in a separate php file and run a cronjob every week.

 

But thanks again for putting me on the right track.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.