Skip to content
Advertisement

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

I call a Woocommerce method $order->get_date_created() that is returning this WC_DateTime object:

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" }

How can I check if there is more (or less) than 24 hours are gone since the order is made?

Advertisement

Answer

Here is the way to check if there is more (or less) than 24 hours passed since an order has been created, using WC_DateTime and DateTime methods on Order created date time:

$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>';
}   
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement