Wendi Posted May 22, 2013 Share Posted May 22, 2013 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 Quote Link to comment Share on other sites More sharing options...
DaveyK Posted May 22, 2013 Share Posted May 22, 2013 This will only trigger if the time is exactly sunday at 00:00:00, which is pretty specific. Why do you do this anyway? Quote Link to comment Share on other sites More sharing options...
Eiseth Posted May 22, 2013 Share Posted May 22, 2013 You can not compare date and string using == Try casting $today to string if($count=='' || (string) $today === 'Sunday 00:00:00') Quote Link to comment Share on other sites More sharing options...
Wendi Posted May 22, 2013 Author Share Posted May 22, 2013 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. Quote Link to comment Share on other sites More sharing options...
Psycho Posted May 22, 2013 Share Posted May 22, 2013 (edited) 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 May 22, 2013 by Psycho Quote Link to comment Share on other sites More sharing options...
Wendi Posted May 22, 2013 Author Share Posted May 22, 2013 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. Quote Link to comment Share on other sites More sharing options...
Eiseth Posted May 22, 2013 Share Posted May 22, 2013 The "date" $today is a string. $today = date('l H:i:s'); Oh sorry thought it return date object, my bad Quote Link to comment Share on other sites More sharing options...
Wendi Posted May 22, 2013 Author Share Posted May 22, 2013 Thank you Psycho for the reply. It makes sense. I'm going to puzzle a bit with the ideas above and see what works for me. Thank you all Quote Link to comment Share on other sites More sharing options...
Solution Wendi Posted May 23, 2013 Author Solution Share Posted May 23, 2013 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. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.