Subtract Time in PHP

Subtract time in PHP

A bit nicer is the following:


$a = new DateTime('08:00');
$b = new DateTime('16:00');
$interval = $a->diff($b);

echo $interval->format("%H");

That will give you the difference in hours.

Time Subtract in PHP

Check it out:

if use $date2 as 24:00

$date1 = new DateTime('02:40');
$date2 = new DateTime('24:00');
$finaldate = $date2->diff($date1);
echo $finaldate->format('%h:%i'); // 21:20

But if use $date2 as 00:00

$date1 = new DateTime('02:40');
$date2 = new DateTime('00:00');
$finaldate = $date2->diff($date1);
echo $finaldate->format('%h:%i'); //02:40

PHP time subtraction from 2 variables

Use php timediff function.

$datetime1 = new DateTime($entryscan);
$datetime2 = new DateTime($exitscan);
$interval = $datetime1->diff($datetime2);
echo $interval->format('%H hours');

%H will give you the difference in hours.

%I will give you the difference in minutes.

%S will give you the difference in seconds.

Subtract Time String in PHP

You need to convert it to a time, subtract 1 second and reformat, e.g.:

$timeLeft = '00:02:30';
$time = DateTime::createFromFormat('H:i:s', $timeLeft);
$time->sub(new DateInterval('PT1S'));
$timeLeft = $time->format('H:i:s');

Below is dirty code that performs the transformation "manually" by converting the time into seconds in case PHP 5.3+ is not available. It'll certainly misbehave it the number of seconds subtracted is greater than the total.

$timeLeft = '00:02:30';
list($hours, $minutes, $seconds) = explode(':', $timeLeft);
$seconds += $hours*3600 + $minutes*60;
unset($hours, $minutes);
$seconds -= 1; //subtraction
$hours = floor($seconds/3600);
$seconds %= 3600;
$minutes = floor($seconds/60);
$seconds %= 60;
$timeLeft = sprintf("%'02u:%'02u:%'02u", $hours, $minutes, $seconds);

PHP - time minus time to minutes

Here you go:

( strtotime('12:45:00') - strtotime('11:00:00') ) / 60

strtotime() is a very useful function. It returns the Unix timestamp for a wide variety of times and dates. So, if you take the two timestamps, and subtract them, then you have the difference in seconds. Divide by 60 to get the minutes.

php adding and subtracting time

update

Since you changed your question...
Looking at the syntax for DateTime::format(),

echo $interval->format("H.i");

should give you the formatting you want.

old answer

The easiest way to deal with time is by representing each time as a number of seconds. (If it's a date, use the number of seconds since the epoch.) You can write some helper functions to convert your times to/from the formats that you want.

For example,

16.00 -> 57600  
7.30 -> 27000
1.00 -> 3600

(57600 - 27000) + 3600 = 34200
34200 -> 9.30

here are the helper functions I came up with:

function timeToSeconds ($time) {
$hours = floor($time);
$minutes = ($time - $hours) * 100;
$seconds = ($hours * 3600) + ($minutes * 60);
return $seconds;
}

function secondsToTime ($seconds) {
$hours = floor($seconds / 3600);
$minutes = ($seconds - ($hours * 3600)) / 60;
$time = "" . $hours . "." . $minutes;
return $time;
}


Related Topics



Leave a reply



Submit