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

Check if there is 24 hours gone since a Woocommerce order is made

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

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

这是检查订单创建后是否超过(或少于)24 小时的方法,在订单创建日期时间使用 WC_DateTimeDateTime 方法:

$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        = $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)

// Output
if ( $diff_in_seconds < $twenty_four_hours ) {
    echo '<p>Order created LESS than 24 hours ago</p>';
} elseif ( $diff_in_seconds = $twenty_four_hours ) {
    echo '<p>Order created 24 hours ago</p>';
} else {
    echo '<p>Order created MORE than 24 hours ago</p>';
}   

要检查订单创建时间是否超过 24 小时,您可以从订单时间戳中减去当前时间戳,然后检查差值是否大于 DAY_IN_SECONDS。

time() - $order->get_date_created()->getTimestamp() > DAY_IN_SECONDS