note 49064 deleted from function.date by tularis

From: Date: Sun, 30 Jan 2005 14:37:36 +0000
Subject: note 49064 deleted from function.date by tularis
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-84217@lists.php.net to get a copy of this message
Note Submitter: kneedie at hotmail dot com ---- 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; } } ?>

« previous php.notes (#84217) next »