Skip to content
Advertisement

Add minutes to time based on current time

I am trying to figure out a way to change a value based on current time, currently I am using the following code to add certain minutes the time the order is placed.

$job_description = 'Order '.$order_id;
$order_created = $order->get_date_created()->format ('Y-m-d H:i:s');

$minutes_to_add = 170;
$order_created_obj = new DateTime($order_created);
$order_created_obj->add(new DateInterval('PT' . $minutes_to_add . 'M'));
$job_pickup_datetime = $order_created_obj->format('Y-m-d H:i:s');

$minutes_to_add = 240;
$order_created_obj = new DateTime($order_created);
$order_created_obj->add(new DateInterval('PT' . $minutes_to_add . 'M'));
$job_delivery_datetime = $order_created_obj->format('Y-m-d H:i:s');

which is working fine (except for $minutes_to_add = 170 is only adding only 30 minutes to the current time for some reason but I am living with it) BUT then I found a need to change the $minutes_to_add value to different value based on the current time (or mainly the time of order, I wrote the following code and tested on WooCommerce but it is giving critical error and not passing the order details.

// Pickup time

$current_time = get_current_time()->format ('H:i:s');

if ($current_time >= '14:30:00' && $current_time <= '17:00:00') {
    $minutes_to_add = 100;
} elseif ($current_time >= '17:00:00' && $current_time < '18:00:00'){
    $minutes_to_add = 250;
} elseif ($current_time >= '18:00:00' && $current_time < '19:00:00'){
    $minutes_to_add = 300;
} else {
    $minutes_to_add = 170;
}
    
$order_created_obj = new DateTime($order_created);
$order_created_obj->add(new DateInterval('PT' . $minutes_to_add . 'M'));
$job_pickup_datetime = $order_created_obj->format('Y-m-d H:i:s');


// delivery time

$current_time = get_current_time()->format ('H:i:s');

if ($current_time >= '14:30:00' && $current_time <= '17:00:00') {
    $minutes_to_add = 130;
} elseif ($current_time >= '17:00:00' && $current_time < '18:00:00'){
    $minutes_to_add = 280;
} elseif ($current_time >= '18:00:00' && $current_time < '19:00:00'){
    $minutes_to_add = 330;
} else {
    $minutes_to_add = 200;
}

$order_created_obj = new DateTime($order_created);
$order_created_obj->add(new DateInterval('PT' . $minutes_to_add . 'M'));
$job_delivery_datetime = $order_created_obj->format('Y-m-d H:i:s');

Advertisement

Answer

I found the solution, will post it in case someone needs it.

// Order info
$job_description = 'Order '.$order_id;
$order_created = date( 'Y-m-d H:i:s', $order->get_date_created ()->getOffsetTimestamp());
$current_time = date( 'H:i:s', $order->get_date_created ()->getOffsetTimestamp());
$minutes_to_add = 0;

if ($current_time >= "00:00:00" && $current_time < "01:00:00") {
    $minutes_to_add = 600;
} elseif ($current_time >= "01:00:00" && $current_time < "02:00:00"){
    $minutes_to_add = 615;
} elseif ($current_time >= "02:00:00" && $current_time < "03:00:00"){
    $minutes_to_add = 570;
} elseif ($current_time >= "03:00:00" && $current_time < "04:00:00"){
    $minutes_to_add = 525;
} elseif ($current_time >= "23:00:00" && $current_time <= "23:59:59"){
    $minutes_to_add = 720;
} else {
    $minutes_to_add = 30;
}
    
$order_created_obj = new DateTime($order_created);
$order_created_obj->add(new DateInterval('PT' . $minutes_to_add . 'M'));
$job_pickup_datetime = $order_created_obj->format('Y-m-d H:i:s');
$current_time = date( 'H:i:s', $order->get_date_created ()->getOffsetTimestamp());


$minutes_to_add_de = 0;
if ($current_time >= "00:00:00" && $current_time < "01:00:00") {
    $minutes_to_add_de = 630;
} elseif ($current_time >= "01:00:00" && $current_time < "02:00:00"){
    $minutes_to_add_de = 645;
} elseif ($current_time >= "02:00:00" && $current_time < "03:00:00"){
    $minutes_to_add_de = 600;
} elseif ($current_time >= "22:00:00" && $current_time < "23:00:00"){
    $minutes_to_add_de = 795;
} elseif ($current_time >= "23:00:00" && $current_time <= "23:59:59"){
    $minutes_to_add_de = 750;
} else {
    $minutes_to_add_de = 60;
}
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement