0

I have an existing PHP shipping time estimate that I need to edit to add in specific time of day (11:30 GMT+1), not just start of new Date where my server is located.

    var today2 = new Date();
    console.log(today);
    var from = addWorkDays(today, 0);
    from = new Date(from);
    //console.log(from.getDate());
    var to = addWorkDays(today2, 2);
    to = new Date(to);

the var from and var to is where it's changed at the last number

Where and how do I add in time of day?

Thanks team!

  • Do you want to get `new Date()` with respect to the user timezone and not the server time ? – Umer Abbas Mar 26 '20 at 04:01
  • My team processes all orders at 11:30am (GMT+1) daily (excluding weekends). So, the date change should change at the same time globally I guess. Ideally I'd like to have "Order within the next "XTIMEFRAME" and your order will be delivered between DATE1 - DATE2. – FlyingKiwi Mar 26 '20 at 04:15
  • If all your operations on the server will be in the remote timezone; you could set the code timezone as well: date_default_timezone_set ( string $timezone_identifier ) – LimpingNinja Mar 26 '20 at 08:47

1 Answers1

0

Here's what you want to do, as i understand from your question, working solution

$datetime = new DateTime; // current time = server time
$otherTZ  = new DateTimeZone('America/Los_Angeles');
$datetime->setTimezone($otherTZ); // calculates with new TZ now

//Current time
echo $datetime->format('d-m-Y H:i:s T');
echo "\r\n";

//Add 2 days in curent time 
$datetime->add(new DateInterval('P2D'));

//Time 2 days from now
echo $datetime->format('d-m-Y H:i:s T');

Edit To add week days and start every day from a specif time of a specific time zone and add week days to the current date to get the expected date you can use following code, see I use this function another SO answer here. see working solution of the code.

$date = new DateTime();
$date->setTimezone(new DateTimeZone('GMT+1'));
$daily_start_time = $date->format('Y-m-d')." 11:30:00 GMT+0100";

$datetime = new DateTime($daily_start_time); // current time = server time
$otherTZ  = new DateTimeZone('GMT+1');
$datetime->setTimezone($otherTZ); // calculates with new TZ now

//Current time
echo $datetime->format('d-m-Y H:i:s T');
echo "\r\n";

//Add 2 days in curent time 
// $datetime->add(new DateInterval('P2D'));

//Add 2 weekdays in current time
$datetime = addWorkingDays($datetime, 2);

//Time 2 days from now
echo $datetime->format('d-m-Y H:i:s T');

function addWorkingDays($date, $day)
{

    if (!($date instanceof \DateTime) || is_string($date)) {
        $date = new \DateTime($date);
    }

    if ($date instanceof \DateTime) {
        $newDate = clone $date;
    }

    if ($day == 0) {
        return $newDate;
    }

    $i = 1;

    while ($i <= abs($day)) {

        $newDate->modify(($day > 0 ? ' +' : ' -') . '1 day');

        $next_day_number = $newDate->format('N');

        if (!in_array($next_day_number, [6, 7])) {
            $i++;
        }

    }

    return $newDate;

}
Umer Abbas
  • 1,866
  • 3
  • 13
  • 19
  • Ok, so if I have the server time set to GMT+1 TZ, I can use the following?: `//Add 2 days in curent time $datetime->add(new DateInterval('P2D')); 'P2D11H30M'` – FlyingKiwi Mar 26 '20 at 04:34
  • I think I might be best to go with something like this instead of getting too deep. https://wordpress.org/plugins/estimate-delivery-date-for-woocommerce/ Any recommendations? – FlyingKiwi Mar 26 '20 at 04:36
  • @FlyingKiwi you could use any solution that work for you, I have not much experience with wordpress so I can't really say. btw i started to solve this so i edited my answr to fit your problem. please a have a look. – Umer Abbas Mar 26 '20 at 04:51
  • Great, thanks Omar. I see the changes and will give this a go! – FlyingKiwi Mar 26 '20 at 09:03