note 102671 added to function.time

From: Date: Mon, 28 Feb 2011 09:11:01 +0000
Subject: note 102671 added to function.time
Groups: php.notes 
Request: Send a blank email to php-notes+get-177387@lists.php.net to get a copy of this message
I took yasmary at gmail dot com's function and updated it a little bit to give it a true facebook style output (yesterday if 1 day ago, the day of the week if within the last week, and then the month day and year (if necessary) for times greater than a week ago). I also tweaked it to adjust the time according to a valid timezone string (DateTimeZone). <?php function time_ago($date,$timezone='America/Denver') { if(empty($date)) { return "No date provided"; } $periods = array("second", "minute", "hour", "day"); $lengths = array("60","60","24","7"); $now = time(); $unix_date = $date; // check validity of date if(empty($unix_date)) { return "Bad date"; } // state the tense $tense = "ago"; 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}"; /* If the return is days, we do some special processing */ if ($j == 3) { // set the timezone using DateTime $timezone = new DateTimeZone($timezone); $dateTime = new DateTime(); $dateTime->setTimestamp($unix_date); $dateTime->setTimezone($timezone); $nowDateTime = new DateTime(); $nowDateTime->setTimestamp($now); $nowDateTime->setTimezone($timezone); if ($difference == 1) { $return = 'Yesterday'; } else if ($difference <= 7) { //$return = strftime('%A',$unix_date); $return = $dateTime->format('l'); } else { //if (strftime('%Y',$unix_time) == strftime('%Y',$now)) { if ($dateTime->format('Y') == $nowDateTime->format('Y')) { //$return = strftime('%B %e',$unix_date); $return = $dateTime->format('F j'); } else { //$return = strftime('%B %e, %Y',$unix_date); $return = $dateTime->format('F j, Y'); } } //$return .= ' at '.strftime('%l:%M%P',$unix_date); $return .= ' at '.$dateTime->format('g:ia'); } return $return; } ?> And if you don't care about the timezone: <?php function time_ago($date) { if(empty($date)) { return "No date provided"; } $periods = array("second", "minute", "hour", "day"); $lengths = array("60","60","24","7"); $now = time(); $unix_date = $date; // check validity of date if(empty($unix_date)) { return "Bad date"; } // state the tense $tense = "ago"; 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}"; /* If the return is days, we do some special processing */ if ($j == 3) { if ($difference == 1) { $return = 'Yesterday'; } else if ($difference <= 7) { $return = strftime('%A',$unix_date); } else { if (strftime('%Y',$unix_date) == strftime('%Y',$now)) { $return = strftime('%B %e',$unix_date); } else { $return = strftime('%B %e, %Y',$unix_date);; } } $return .= ' at '.strftime('%l:%M%P',$unix_date); } return $return; } ?> Returns look like this: 33 minutes ago 1 hour ago Yesterday at 11:06am Tuesday at 2:39pm February 18 at 8:53pm December 19, 2010 at 4:46pm ---- Server IP: 69.147.83.197 Probable Submitter: 98.206.139.56 ---- Manual Page -- http://www.php.net/manual/en/function.time.php Edit -- https://master.php.net/note/edit/102671 Del: integrated -- https://master.php.net/note/delete/102671/integrated Del: useless -- https://master.php.net/note/delete/102671/useless Del: bad code -- https://master.php.net/note/delete/102671/bad+code Del: spam -- https://master.php.net/note/delete/102671/spam Del: non-english -- https://master.php.net/note/delete/102671/non-english Del: in docs -- https://master.php.net/note/delete/102671/in+docs Del: other reasons-- https://master.php.net/note/delete/102671 Reject -- https://master.php.net/note/reject/102671 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#177387) next »