检查下 Woocommerce 订单是否已经过去 24 小时

我调用了一个$order->get_date_created()返回这个WC_DateTime对象的 Woocommerce 方法:

object(WC_DateTime)#26619 (4) { ["utc_offset":protected]=> int(0) ["date"]=> string(26) "2019-09-06 14:28:17.000000" ["timezone_type"]=> int(3) ["timezone"]=> string(16) "Europe/Amsterdam" }

我如何检查自下订单以来是否超过(或少于)24 小时?


汪汪一只猫
浏览 123回答 2
2回答

阿波罗的战车

这是检查是否有更多(或更少)超过24小时,因为一个订单已创建,使用传递的方式WC_DateTime,并DateTime在订购方法创建日期时间:$order = wc_get_order($order_id); // Get an instance of the WC_Order Object$date_created_dt = $order->get_date_created(); // Get order date created WC_DateTime Object$timezone&nbsp; &nbsp; &nbsp; &nbsp; = $date_created_dt->getTimezone(); // Get the timezone$date_created_ts = $date_created_dt->getTimestamp(); // Get the timestamp in seconds$now_dt = new WC_DateTime(); // Get current WC_DateTime object instance$now_dt->setTimezone( $timezone ); // Set the same time zone$now_ts = $now_dt->getTimestamp(); // Get the current timestamp in seconds$twenty_four_hours = 24 * 60 * 60; // 24hours in seconds$diff_in_seconds = $now_ts - $date_created_ts; // Get the difference (in seconds)// Outputif ( $diff_in_seconds < $twenty_four_hours ) {&nbsp; &nbsp; echo '<p>Order created LESS than 24 hours ago</p>';} elseif ( $diff_in_seconds = $twenty_four_hours ) {&nbsp; &nbsp; echo '<p>Order created 24 hours ago</p>';} else {&nbsp; &nbsp; echo '<p>Order created MORE than 24 hours ago</p>';}&nbsp; &nbsp;

慕容3067478

要检查订单是否在 24 小时前创建,您可以从订单时间戳中减去当前时间戳,然后检查该差异是否大于 DAY_IN_SECONDS。time()&nbsp;-&nbsp;$order->get_date_created()->getTimestamp()&nbsp;>&nbsp;DAY_IN_SECONDS
打开App,查看更多内容
随时随地看视频慕课网APP