Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

php date_diff not working with time in h:i A format

Tags:

php

I have an existing time in DB in h:i A format Ex: 6.30 AM I am determining the current time as follows:

$datecalc = new DateTime("now", new DateTimeZone($time_zone));
$timesnow = $datecalc->format('h:i A'); //09.00 AM
//echo $timesnow;

But the following is not working??

$interval = date_diff($timesnow, $time_in_DB);
echo $interval; //NO RESULT

$time_in_DB is also in h:i A format.

Why is it not working or am i missing something as a newbie ??

like image 688
Pamela Avatar asked Jan 28 '26 13:01

Pamela


1 Answers

The date_diff function accepts date objects not strings Here is the correct example :

$date1=date_create("9:00 AM");
$date2=date_create("6:00 PM");
$diff=date_diff($date1,$date2);
echo $diff->format("%h");

it will print 9 hours difference

like image 105
Abid Nawaz Avatar answered Jan 31 '26 01:01

Abid Nawaz



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!