gpt4 book ai didi

php date_diff 不能与 h :i A format 中的时间一起使用

转载 作者:行者123 更新时间:2023-12-04 02:00:51 24 4
gpt4 key购买 nike

我在 DB 中有一个现有时间,格式为 h:i A Ex: 6.30 AM我确定当前时间如下:

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

但是下面的不起作用??

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

$time_in_DB 也是 h:i A 格式。

为什么它不起作用或者我作为新手错过了什么??

最佳答案

date_diff 函数接受日期对象而不是字符串这是正确的例子:

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

它将打印 9 小时的差异

关于php date_diff 不能与 h :i A format 中的时间一起使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47396530/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com