/** * Time difference calculation * * @param Timestamp $time * @return String time Elapsed*/ functionTime2units ($time,$sign) { $year= Floor($time/60/60/24/365); $time-=$year* 60 * 60 * 24 * 365; $month= Floor($time/60/60/24/30); $time-=$month* 60 * 60 * 24 * 30; $week= Floor($time/60/60/24/7); $time-=$week* 60 * 60 * 24 * 7; $day= Floor($time/60/60/24); $time-=$day* 60 * 60 * 24; $hour= Floor($time/60/60); $time-=$hour* 60 * 60; $minute= Floor($time/60); $time-=$minute* 60; $second=$time; $elapse= ' '; $UNITARR=Array( ' Year ' = ' years ', ' months ' = ' month ', ' Week ' = ' Week ', ' days ago ' = ' Day ', ' hours ' =& gt; ' Hour ', ' minutes ' = ' minute ', ' just ' ' = ' second ' ); foreach($UNITARR as $CN=$u ) { if( $$u> 0 ) { if($sign= = ' Day '){ $elapse= $$u.$CN; Break; }Else{ //the last two lines only take the maximum time (there is a minute to give up the second, an hour to give up the seconds, etc.) $elapse.= $$u.$CN; } } } return $elapse; }}
PHP Time Difference method