Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP: subtract time

Tags:

php

Here's my code for subtracting a time...but the problem here its not subtracting also the seconds..

fldFullTime and fldTotalDuration are came from in my database..

Assuming the fldFullTime is 10:00:00

then for fldTotalDuration is 08:30:00

$fulltime = new DateTime($row['fldFullTime']);
$totalduration = new DateTime($row['fldTotalDuration']);

$res= $fulltime ->diff($totalduration);
echo "DIFF: ".$res->format("%H:%S");

The result the I get for this code is 01:00

So, when I subtract the fldFullTime [10:00:00] to fldTotalDuration [08:30:00] the result should be is 1:30:00

How can I make it?

Thanks for the help

like image 399
user2826499 Avatar asked Apr 27 '26 09:04

user2826499


2 Answers

You're using a wrong format string :

  • %H : hours
  • %S : seconds

You need to add minutes :

$fulltime = new DateTime('10:00:00');
$totalduration = new DateTime('08:30:00');
$res= $fulltime ->diff($totalduration);

echo "DIFF: ".$res->format("%H:%S");
//DIFF: 01:00
echo "DIFF: ".$res->format("%H:%I:%S");
//DIFF: 01:30:00
like image 68
zessx Avatar answered Apr 28 '26 21:04

zessx


check this :

  $fulltime = new DateTime('10:00:00');
$totalduration = new DateTime('08:30:00');

$res= $fulltime ->diff($totalduration);
echo "DIFF: ".$res->format("%H:%I:%S");
like image 32
Mahmood Rehman Avatar answered Apr 28 '26 23:04

Mahmood Rehman



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!