How to create time difference like 1 day ago in php??
Time difference function to display particular records added x mins ago!
Calculate the difference in time between a past date and a future date
A time difference function that outputs the time passed in facebook’s style: 1 day ago, or 4 months ago.
This is really cool function, you dont need to do any just pass value( your db date) to this function and it returns “ago time”. easy isn’t it ?
<?php function Agotime($date) { if(empty($date)) { return "No date provided"; } $periods = array("second", "minute", "hour", "day", "week", "month", "year", "decade"); $lengths = array("60","60","24","7","4.35","12","10"); $now = time(); $unix_date = strtotime($date); // check validity of date if(empty($unix_date)) { return "Bad date"; } // is it future date or past date if($now > $unix_date) { $difference = $now - $unix_date; $tense = "ago"; } else { $difference = $unix_date - $now; $tense = "from now"; } for($j = 0; $difference >= $lengths[$j] && $j < count($lengths)-1; $j++) { $difference /= $lengths[$j]; } $difference = round($difference); if($difference != 1) { $periods[$j].= "s"; } return "$difference $periods[$j] {$tense}"; } $date = "2009-03-04 17:45"; $result = Agotime($date); // 2 days ago ?>
add Agotime(); function in page any there or any include page, and just user
echo Agotime($data['recordDate']); //// and it display ago time itself
Leave comments!!














November 18th, 2009 at 3:17 am
I think there is a bug, if you pass a time minute before it shows 5 hours ago
November 18th, 2009 at 6:03 am
rv, you must know about php date requiments. this function not shows your pc date untill you set TimeZone
add this on script
if you are on USA
date_default_timezone_set(’America/Los_Angeles’);
or if you in india
date_default_timezone_set(’Asia/Calcutta’);
more see this page
http://www.php.net/manual/en/timezones.php
February 2nd, 2010 at 12:06 pm
Hi,
I happened to see your post find it quite informative. I would like to share a link where a software engineer has shared a tip on “Time Difference problem in PHP and MySQL and its solution”. I am sharing it just for the knowledge purpose.
Here is the link:
http://www.mindfiresolutions.com/Time-Difference-problem-in-PHP-and-MySQL-and-its-solution-534.php
Hope you find it useful and of assistance.
Thanks,
Bijayani
February 3rd, 2010 at 6:36 am
Bijayani,
thanks for sharing this, but you need to put our website url on that page also. so user must know this is based on our post. otherwise we wont allow you to do that. as per our policy.. i guess you understand this
August 22nd, 2011 at 6:23 am
How to rotate my blog posts over and over with a preset time interval with PHP code?
October 10th, 2011 at 3:04 am
Rather than doing it in PHP, its better to do it using JavaScript. Check out the following post http://tinywall.info/2011/10/09/facebook-like-friendly-time-with-gmt-and-utc-in-web-application-with-php-javascript/
December 21st, 2011 at 11:30 am
Thanks for this nice code.