note 74157 added to function.gmdate

From: Date: Wed, 28 Mar 2007 02:53:06 +0000
Subject: note 74157 added to function.gmdate
Groups: php.notes 
Request: Send a blank email to php-notes+get-124862@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. <? 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 ---- Server IP: 194.145.210.3 Probable Submitter: 80.3.64.11 (proxied: 80.4.197.80) ---- Manual Page -- http://www.php.net/manual/en/function.gmdate.php Edit -- https://master.php.net/note/edit/74157 Del: integrated -- https://master.php.net/note/delete/74157/integrated Del: useless -- https://master.php.net/note/delete/74157/useless Del: bad code -- https://master.php.net/note/delete/74157/bad+code Del: spam -- https://master.php.net/note/delete/74157/spam Del: non-english -- https://master.php.net/note/delete/74157/non-english Del: in docs -- https://master.php.net/note/delete/74157/in+docs Del: other reasons-- https://master.php.net/note/delete/74157 Reject -- https://master.php.net/note/reject/74157 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#124862) next »