开发者

How to find earliest upcoming date given a list of weekdays?

开发者 https://www.devze.com 2023-04-03 06:43 出处:网络
Dates in PHP are a nightmare for me so please help me out fellow coders... I want to notify customers about the day their order will be delivered. It works like this:

Dates in PHP are a nightmare for me so please help me out fellow coders... I want to notify customers about the day their order will be delivered. It works like this:

I have 2 shipping zones, A & B. Orders for zone A are delivered each Monday, Wednesday & Friday, whereas zone B is on Tuesday, Thursday, Saturday. For each order, the delivery day is scheduled for the NEX开发者_开发百科T AVAILABLE day, depending on the zone. Please consider that if someone places an order on Monday the goods will be delivered on the NEXT available date, that would be Tuesday for zone B and Wednesday for zone A.

How can I calculate the NEXT AVAILABLE delivery date and notify the customer?

Thanks.


This will certainly not be the fastest or most clever answer, but it's going to be a pleasure to read the code.

Assuming we are shipping in zone A:

$dates = array(
    new DateTime('next monday'), // magic!
    new DateTime('next wednesday'),
    new DateTime('next friday'),
);

// Seems to work because since PHP 5.2.2 DateTime objects
// can be compared with the < and > operators    
$shippingDate = min($dates);

echo $shippingDate->format('Y-m-d');

You might want to take a look at the relative date formats available in PHP, this is the part where the "next monday" magic happens. For information on what you can do with $shippingDate, see the documentation on class DateTime.

Update

For completeness, here is a more old-school version which does not need PHP 5.3 and should also be faster (although speed is practically irrelevant here). I don't like it as much, because it's not easy to verify that it works correctly. In contrast to the version above, this one had a bug when I first wrote it. Simple is good.

$today = date('w');

// These values are from http://www.php.net/manual/en/function.date.php
$shippingDays = array(
    1, // mon
    3, // wed
    5, // fri
);

// Each of these weekdays is how many days into the future?
foreach($shippingDays as &$day) {
    $day = (7 + $day - $today) % 7;
}

// Get the earliest one, but not if it's today
// array_filter is used to remove a possible 0
$daysInFuture = min(array_filter($shippingDays));
$shippingDate = new DateTime('+'.$daysInFuture.' days');
echo $shippingDate->format('Y-m-d');

See it in action.


Try this:

// Info
$date = array(date("d"), date("m"), date("Y"));
$zone = "A";
// ------

$zones = array("A" => array(1 => "Monday",    
                            3 => "Wednesday", 
                            5 => "Friday")     

              ,"B" => array(2 => "Tuesday",    
                            4 => "Thursday",   
                            6 => "Saturday")); 

$found = false;
$days_plus = 1; // always next day

// Retrieve last day from the zone
end($zones[$zone]);
$last_day = key($zones[$zone]);

do {
    $mk = mktime(0, 0, 0, $date[1], ($date[0] + $days_plus), $date[2]);
    $week = date("w", $mk);

    // if week not passed last day of zone
    if ($week <= $last_day)
    {
        if (!isset($zones[$zone][$week]))
        {
            $days_plus++;
        }
        else
        {
            $found = true;
        }
    }
    else
    {
        $days_plus++;
    }
} while (!$found);

echo "Next date: " . date("d/m/Y - l", $mk);


$timestamp = strtotime('next Monday');
$date      = date('Y-m-d', $timestamp);
0

精彩评论

暂无评论...
验证码 换一张
取 消