Lambneck Posted May 26, 2009 Share Posted May 26, 2009 Hello, I was wondering does anyone knows how to create something to track the time/date of a blog post? But a dynamic one that works like the one used on twitter where it shows recent posts as something like "2 minutes ago" or "6 hours ago", and then shows older posts as the time and date ex. "12:00 PM May 25th". Quote Link to comment https://forums.phpfreaks.com/topic/159668-solved-dynamic-date-tracker-for-blog-posts/ Share on other sites More sharing options...
JonnoTheDev Posted May 26, 2009 Share Posted May 26, 2009 The time of the post is recorded in the database. The date calculation uses this field to display how long ago the post was created. Easy. You would use a database query to obtain the most recent posts if you liked using the date field. Quote Link to comment https://forums.phpfreaks.com/topic/159668-solved-dynamic-date-tracker-for-blog-posts/#findComment-842138 Share on other sites More sharing options...
Lambneck Posted May 26, 2009 Author Share Posted May 26, 2009 Ok, but i want the way the time is displayed to change based on how old the post is. Within 24 hours it would display ex. "6 hours ago". After twenty four hours it would display ex. "1:30 PM May 20th". Im not sure if this is a php question, if it can be done with PHP or not. Quote Link to comment https://forums.phpfreaks.com/topic/159668-solved-dynamic-date-tracker-for-blog-posts/#findComment-842178 Share on other sites More sharing options...
JonnoTheDev Posted May 26, 2009 Share Posted May 26, 2009 Of Course. You can write a function to display the date from the database record. You should record as a datetime or a unix timestamp Quote Link to comment https://forums.phpfreaks.com/topic/159668-solved-dynamic-date-tracker-for-blog-posts/#findComment-842211 Share on other sites More sharing options...
Lambneck Posted May 26, 2009 Author Share Posted May 26, 2009 I know how to record the time/date to database and then display it, what Im trying to do here is make it so the way it is displayed changes depending upon whether it is a recent post or not. recent posts being displayed as "...hours ago" and then older posts display simply as their dates they were posted. how to have the time display as "...hours ago" change to date after 24 hours? Quote Link to comment https://forums.phpfreaks.com/topic/159668-solved-dynamic-date-tracker-for-blog-posts/#findComment-842237 Share on other sites More sharing options...
gevans Posted May 26, 2009 Share Posted May 26, 2009 <?php $postedtime = //the time from db as timestamp? $time_difference = time() - $postedtime; if($time_difference < (60*60*42)) { //less than 24 hours } else { //more than 24 hours } Quote Link to comment https://forums.phpfreaks.com/topic/159668-solved-dynamic-date-tracker-for-blog-posts/#findComment-842239 Share on other sites More sharing options...
JonnoTheDev Posted May 26, 2009 Share Posted May 26, 2009 There you go. gevans has given some basic code. Just turn it into a function i.e. function postTime($time) { return $string; } Quote Link to comment https://forums.phpfreaks.com/topic/159668-solved-dynamic-date-tracker-for-blog-posts/#findComment-842256 Share on other sites More sharing options...
gevans Posted May 26, 2009 Share Posted May 26, 2009 Just in case no one notices I put in 42 instead of 24 (there aren't 42 hours in a day) Quote Link to comment https://forums.phpfreaks.com/topic/159668-solved-dynamic-date-tracker-for-blog-posts/#findComment-842260 Share on other sites More sharing options...
Lambneck Posted May 26, 2009 Author Share Posted May 26, 2009 ugh, I'm not sure I understand. Should it look anything like the following? What to echo when it is "//less than 24 hours"? <?php $postedtime = time() $time_difference = time() - $postedtime; if($time_difference < (60*60*24)) { //less than 24 hours } else { echo date("M dS, Y", $row['date']); //more than 24 hours } Quote Link to comment https://forums.phpfreaks.com/topic/159668-solved-dynamic-date-tracker-for-blog-posts/#findComment-842278 Share on other sites More sharing options...
JonnoTheDev Posted May 26, 2009 Share Posted May 26, 2009 correct Quote Link to comment https://forums.phpfreaks.com/topic/159668-solved-dynamic-date-tracker-for-blog-posts/#findComment-842281 Share on other sites More sharing options...
Lambneck Posted May 26, 2009 Author Share Posted May 26, 2009 But how to echo the amount of time thats passed since its post? //less than 24 hours Quote Link to comment https://forums.phpfreaks.com/topic/159668-solved-dynamic-date-tracker-for-blog-posts/#findComment-842318 Share on other sites More sharing options...
gevans Posted May 26, 2009 Share Posted May 26, 2009 You should just have a play, its not hard to work out, here's a start; <?php echo ceil($time_difference/60); //prints the number of seconds rounded up echo ceil($time_difference/3600); //prints the number of minutes rounded up Quote Link to comment https://forums.phpfreaks.com/topic/159668-solved-dynamic-date-tracker-for-blog-posts/#findComment-842325 Share on other sites More sharing options...
JonnoTheDev Posted May 26, 2009 Share Posted May 26, 2009 Look at the php function manual examples for date / time differences http://uk2.php.net/manual/en/function.date.php Quote Link to comment https://forums.phpfreaks.com/topic/159668-solved-dynamic-date-tracker-for-blog-posts/#findComment-842333 Share on other sites More sharing options...
Lambneck Posted May 26, 2009 Author Share Posted May 26, 2009 do i need to put more "if" or "else" statements inbetween them or just like the following? <?php $postedtime = time() $time_difference = time() - $postedtime; if($time_difference < (60*60*24)) { //less than 24 hours echo (ceil($time_difference/60) . "seconds ago"); //prints the number of SECONDS rounded up echo (ceil($time_difference/3600) . "minutes ago"); //prints the number of MINUTES rounded up echo (ceil($time_difference/21600) . "hours ago"); //prints the number of HOURS rounded up } else { echo date("M dS, Y", $row['date']); //more than 24 hours } Quote Link to comment https://forums.phpfreaks.com/topic/159668-solved-dynamic-date-tracker-for-blog-posts/#findComment-842400 Share on other sites More sharing options...
gevans Posted May 26, 2009 Share Posted May 26, 2009 That depends on what you want to print out, I do wish you would at least attempt some of this yourself, see neil's last post http://www.phpfreaks.com/forums/index.php/topic,253898.msg1193148.html#msg1193148 Just for your information I messed up the commenting echo (ceil($time_difference) . " seconds ago"); //prints the number of SECONDS rounded up echo (ceil($time_difference/60) . " minutes ago"); //prints the number of MINUTES rounded up echo (ceil($time_difference/1440) . " hours ago"); //prints the number of HOURS rounded up Quote Link to comment https://forums.phpfreaks.com/topic/159668-solved-dynamic-date-tracker-for-blog-posts/#findComment-842415 Share on other sites More sharing options...
Lambneck Posted May 26, 2009 Author Share Posted May 26, 2009 Sorry, Im trying, but not getting it yet. what about this? <?php function postTime($time) { if($time_difference < (60*60*24)) { //less than 24 hours echo (ceil($time_difference) . " seconds ago"); //prints the number of SECONDS rounded up echo (ceil($time_difference/60) . " minutes ago"); //prints the number of MINUTES rounded up echo (ceil($time_difference/1440) . " hours ago"); //prints the number of HOURS rounded up } else { echo date("M dS, Y", $row['date']); //more than 24 hours } return $time; } Quote Link to comment https://forums.phpfreaks.com/topic/159668-solved-dynamic-date-tracker-for-blog-posts/#findComment-842435 Share on other sites More sharing options...
JonnoTheDev Posted May 26, 2009 Share Posted May 26, 2009 No, miles off and those functions wont work! Use this example and try to learn the date & time functions from the php manual <?php function timeDiff($time1) { $time2 = time(); $diff = array('years' => 0, 'months' => 0, 'weeks' => 0, 'days' => 0, 'hours' => 0, 'minutes' => 0, 'seconds' => 0); foreach(array('years','months','weeks','days','hours','minutes','seconds') as $unit) { while(true) { $next = strtotime("+1 $unit", $time1); if($next < $time2) { $time1 = $next; $diff[$unit]++; } else { break; } } } return($diff); } $postTime = strtotime("-2 hours"); $diffArray = timeDiff($postTime); print "<pre>"; print_r($diffArray); print "</pre>"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/159668-solved-dynamic-date-tracker-for-blog-posts/#findComment-842461 Share on other sites More sharing options...
Lambneck Posted May 26, 2009 Author Share Posted May 26, 2009 Ok thanks, I'll play around with it. Appreciate all the help. Quote Link to comment https://forums.phpfreaks.com/topic/159668-solved-dynamic-date-tracker-for-blog-posts/#findComment-842495 Share on other sites More sharing options...
Lambneck Posted May 27, 2009 Author Share Posted May 27, 2009 Still not getting the amount of time to display ??? using: <?php function timeDiff($time1) { $time2 = time(); $diff = array('years' => 0, 'months' => 0, 'weeks' => 0, 'days' => 0, 'hours' => 0, 'minutes' => 0, 'seconds' => 0); foreach(array('years','months','weeks','days','hours','minutes','seconds') as $unit) { while(true) { $next = strtotime("+1 $unit", $time1); if($next < $time2) { $time1 = $next; $diff[$unit]++; } else { break; } } } return($diff); } $postTime = strtotime("-2 hours"); $diffArray = timeDiff($postTime); print "<pre>"; print_r($diffArray); print "</pre>"; ?> displays: Array ( [years] => 0 [months] => 0 [weeks] => 0 [days] => 0 [hours] => 1 [minutes] => 59 [seconds] => 59 ) Quote Link to comment https://forums.phpfreaks.com/topic/159668-solved-dynamic-date-tracker-for-blog-posts/#findComment-842801 Share on other sites More sharing options...
JonnoTheDev Posted May 27, 2009 Share Posted May 27, 2009 Yes. Finish it off. Use the array to construct the time. Quote Link to comment https://forums.phpfreaks.com/topic/159668-solved-dynamic-date-tracker-for-blog-posts/#findComment-842954 Share on other sites More sharing options...
Lambneck Posted May 31, 2009 Author Share Posted May 31, 2009 Ah! So frustrating! ??? I dont know how to do it. Ive been comparing what you wrote to the way its shown in the manual (http://cn.php.net/time), but I still cant figure it out. I guess I'm too new to php for this type of thing. Have any recommended reading that might help me out with this matter? Quote Link to comment https://forums.phpfreaks.com/topic/159668-solved-dynamic-date-tracker-for-blog-posts/#findComment-846078 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.