Jump to content

Date based condition


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
https://forums.phpfreaks.com/topic/278279-date-based-condition/
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.

 

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.