简体   繁体   中英

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

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 ??

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

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