note 49064 added to function.date

From: Date: Sat, 15 Jan 2005 21:43:07 +0000
Subject: note 49064 added to function.date
Groups: php.notes 
Request: Send a blank email to php-notes+get-83350@lists.php.net to get a copy of this message
I needed a function that could take two timestamps en return the time between them, but I wasn't able to find it here so I made it myself. I think it's very useful, because it takes two MySQL Timestamps. <?php function users_timestamp_dif($time_first, $time_last, $type=0) { /* This function takes 2 or 3 arguments and returns the time between 2 MySQL timestamps type = 0 :: returns a total string with years, days, hours, minutes, seconds. type = 1 :: returns only the years type = 2 :: returns only the days type = 3 :: returns only the hours type = 4 :: returns only the minutes type = 5 :: returns only the seconds */ $time_string = array( "years" => "jaren", "days" => "dagen", "hours" => "uren", "minutes" => "minuten", "seconds" => "seconds" ); $years_last = substr($time_last, 0, 4); $months_last = substr($time_last, 4, 2); $days_last = substr($time_last, 6, 2); $hours_last = substr($time_last, 8, 2); $minutes_last = substr($time_last, 10, 2); $seconds_last = substr($time_last, 12, 2); $years_first = substr($time_first, 0, 4); $months_first = substr($time_first, 4, 2); $days_first = substr($time_first, 6, 2); $hours_first = substr($time_first, 8, 2); $minutes_first = substr($time_first, 10, 2); $seconds_first = substr($time_first, 12, 2); $seconds_last = date("U", mktime($hours_last, $minutes_last, $seconds_last, $months_last, $days_last, $years_last)); $seconds_first = date("U",mktime($hours_first, $minutes_first, $seconds_first, $months_first, $days_first, $years_first)); $seconds_ago = $seconds_last-$seconds_first; $seconds_time = $seconds_ago % 60; $minutes_time = (($seconds_ago-$seconds_time) / 60) % 60; $hours_time = (($seconds_ago-($seconds_time+$minutes_time*60)) / 3600) % 24; $days_time = (($seconds_ago-($seconds_time+$minutes_time*60+$hours_time*3600)) / 86400) % 365; $years_time = ($seconds_ago-($seconds_time+ $minutes_time*60+$hours_time*3600+$days_time*86400)) / 31536000; switch($type) { case 1: return $years_time; break; case 2: return $days_time; break; case 3: return $hours_time; break; case 4: return $minutes_time; break; case 5: return $seconds_time; break; default: return $seconds_time." ".$time_string['seconds'].", ".$minutes_time." ".$time_string['minutes'].", ".$hours_time." ".$time_string['hours'].", ".$days_time." ".$time_string['days'].", ".$years_time." ".$time_string['years']; break; } } ?> ---- Manual Page -- http://www.php.net/manual/en/function.date.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+49064 Delete -- http://master.php.net/manage/user-notes.php?action=delete+49064&report=yes Reason: bad code -- http://master.php.net/manage/user-notes.php?action=delete+49064&report=yes&reason=bad+code Reason: spam -- http://master.php.net/manage/user-notes.php?action=delete+49064&report=yes&reason=spam Reason: useless example -- http://master.php.net/manage/user-notes.php?action=delete+49064&report=yes&reason=useless+example Reason: contains commercial links -- http://master.php.net/manage/user-notes.php?action=delete+49064&report=yes&reason=contains+commercial+links Reason: useless note -- http://master.php.net/manage/user-notes.php?action=delete+49064&report=yes&reason=useless+note Reject -- http://master.php.net/manage/user-notes.php?action=reject+49064&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#83350) next »