skip to Main Content

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?

2

Answers


  1. 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>';
    }   
    
    Login or Signup to reply.
  2. To check if an order was created more than 24 hours ago, you can subtract the current timestamp from the order timestamp and then check if that difference is greater than DAY_IN_SECONDS.

    time() - $order->get_date_created()->getTimestamp() > DAY_IN_SECONDS
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search