简体   繁体   中英

PHP hours and minutes between two times

$time1 = "14:45";
$time2 = "15:55";
list($hours, $minutes) = explode(':', $time1);
$startTimestamp = mktime($hours, $minutes);

list($hours, $minutes) = explode(':', $time2);
$endTimestamp = mktime($hours, $minutes);

$seconds = $endTimestamp - $startTimestamp;
$minutes = ($seconds / 60) % 60;
$hours = round($seconds / (60 * 60));

echo "<b>$hours</b> hours and<b>$minutes</b> minutes</b>";

This code works good if the variable time1 is lower than variable time2, but if the time1 is "23:35" and time2 is "01:40" it will output "-22 hours and -55 minutes".

How can I correct this so it still will output the hours and minutes between the both times even if the time1 is lower than time2 variable

This code will take two times example "14:45" and "16:33" and calculate how many hours and minutes it is between each time, but it doesn't work if "time1" is lower than "time2" (time1 and time2 is variables)

http://codepad.org/QmFdN6RV

This is how I would do it, using strtotime() :

// If dates are not specified, strtotime() will assume both are from today
$time1 = "23:45";
$time2 = "01:24";

// Absolute value of time difference in seconds
$diff = abs(strtotime($time1) - strtotime($time2));

// Convert $diff to minutes
$tmins = $diff/60;

// Get hours
$hours = floor($tmins/60);

// Get minutes
$mins = $tmins%60;

See demo

This assumes that $time1 and $time2 are from the same day. If not, then this will need to be specified.

$time1 = "today 23:45";
$time2 = "tomorrow 01:24";
// or
$time1 = "2014-7-3 23:45";
$time2 = "2014-7-4 01:24";
// or some other method of specifying the day.

See demo 2

if the seconds are negative, add 24 hours to them:

$time1 = "23:35";
$time2 = "1:40";  
list($hours, $minutes) = explode(':', $time1);
$startTimestamp = mktime($hours, $minutes);

list($hours, $minutes) = explode(':', $time2);
$endTimestamp = mktime($hours, $minutes);

$seconds = $endTimestamp - $startTimestamp;
if($seconds < 0) {
    $seconds+=60*60*24; 
}

$minutes = ($seconds / 60) % 60;
$hours = round($seconds / (60 * 60));

echo "<b>$hours</b> hours and<b>$minutes</b> minutes</b>";

You could use date_diff

$date1 = new DateTime();
$date2 = new DateTime();
$date1->setTimestamp($startTimestamp);
$date2->setTimestamp($endTimestamp);
$interval = date_diff($date1, $date2);
//or $interval = $date1->diff($date2)
echo $interval->format('%h hours and %m minutes');

You would do this way:

$time1 = "23:40";
$time2 = "01:40";
list($hours, $minutes) = explode(':', $time1);
$startTimestamp = mktime($hours, $minutes);

list($hours, $minutes) = explode(':', $time2);
$endTimestamp = mktime($hours, $minutes);

$seconds = abs($endTimestamp - $startTimestamp);
$diff = (24*60*60)-$seconds;
if ($diff < $seconds) {
    $seconds = $diff;
}

$minutes = ($seconds / 60) % 60; 
$hours = round($seconds / (60 * 60));

echo "<b>$hours</b> hours and<b>$minutes</b> minutes</b>"

Note that this will never give you diferences bigger than 12h.

For instance, the difference between 06:00 and 22:00 will be 8 hours

So dont know what you really want as result - could be two ways, so i do both ;)

you have also to use floor and not round.

First solution is, that you get the difference based on same day:

<?php
$time1 = "23:45";
$time2 = "01:44";
list($hours, $minutes) = explode(':', $time1);
$firstTimestamp = mktime($hours, $minutes);

list($hours, $minutes) = explode(':', $time2);
$secondTimestamp = mktime($hours, $minutes);

$firstTimestamp > $secondTimestamp?$seconds = $firstTimestamp - $secondTimestamp:$seconds = $secondTimestamp - $firstTimestamp;

$minutes = ($seconds / 60) % 60;
$hours = floor($seconds / (60 * 60));

echo "<b>$hours</b> hours and <b>$minutes</b> minutes</b>";
?>

output would be: hours and minutes 小时分钟

the second way is, that the endtime (named it secondtime) is the next day than it should be like

<?php
$time1 = "23:45";
$time2 = "01:24";
list($hours, $minutes) = explode(':', $time1);
$firstTimestamp = mktime($hours, $minutes);

list($hours, $minutes) = explode(':', $time2);
$secondTimestamp = mktime($hours, $minutes);

$seconds =  $secondTimestamp - $firstTimestamp;

$seconds<0?$seconds+=24*60*60:"";

$minutes = ($seconds / 60) % 60;
$hours = floor($seconds / (60 * 60));


echo "<b>$hours</b> hours and <b>$minutes</b> minutes</b>";
?>

output would be: hours and minutes 小时分钟

url to codepad: http://codepad.org/7plRAhZJ

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM