note 74157 modified in function.gmdate by danbrown

From: Date: Sun, 20 Jul 2008 03:10:33 +0000
Subject: note 74157 modified in function.gmdate by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-142030@lists.php.net to get a copy of this message
I wanted to get the time past from two MySQL dates and came up with this code that does the trick. Supply a start date, end date and optional output date/time format the default is in seconds but will expand from SS to MM:SS and then to HH:MM:SS automatically, you may wish to force a date format that will not be dynamic (site layout etc). See examples below, also see function date() for more date format options. <?php function calculate_time_past($start_time, $end_time, $format = "s") { $time_span = strtotime($end_time) - strtotime($start_time); if ($format == "s") { // is default format so dynamically calculate date format if ($time_span > 60) { $format = "i:s"; } if ($time_span > 3600) { $format = "H:i:s"; } } return gmdate($format, $time_span); } $start_time = "2007-03-28 00:50:14"; // 00:50:14 will work on its own $end_time = "2007-03-28 00:52:59"; // 00:52:59 will also work instead echo calculate_time_past($start_time, $end_time) . "<br />"; // will output 02:45 echo calculate_time_past($start_time, $end_time, "H:i:s"); // will output 00:02:45 when format is overridden ?> I hope it of use. Regards, Ashley --was-- I wanted to get the time past from two MySQL dates and came up with this code that does the trick. Supply a start date, end date and optional output date/time format the default is in seconds but will expand from SS to MM:SS and then to HH:MM:SS automatically, you may wish to force a date format that will not be dynamic (site layout etc). See examples below, also see function date() for more date format options. <? function calculate_time_past($start_time, $end_time, $format = "s") { $time_span = strtotime($end_time) - strtotime($start_time); if ($format == "s") { // is default format so dynamically calculate date format if ($time_span > 60) { $format = "i:s"; } if ($time_span > 3600) { $format = "H:i:s"; } } return gmdate($format, $time_span); } $start_time = "2007-03-28 00:50:14"; // 00:50:14 will work on its own $end_time = "2007-03-28 00:52:59"; // 00:52:59 will also work instead echo calculate_time_past($start_time, $end_time) . "<br />"; // will output 02:45 echo calculate_time_past($start_time, $end_time, "H:i:s"); // will output 00:02:45 when format is overridden ?> I hope it of use. Regards, Ashley http://php.net/manual/en/function.gmdate.php

« previous php.notes (#142030) next »