在城市交货天使用阵列()PHP [英] Delivery days in cities using array () PHP

查看:113
本文介绍了在城市交货天使用阵列()PHP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在两个不同的城市我的网上商店的送货天的安排:

I have an arrangement with the delivery days of my online store in two different cities:

$city1 = array("Monday", "Friday");

$city2 = array("Monday", "Thursday", "Saturday");

这家商店提供每2天,但只取决于城市某些日子。

The store delivers every 2 days, but only on certain days depending on the city.

如果今天是周四和城市1有人买,该订单将在2天内收到在这种情况下,周一(见上文阵列)。

If today is Thursday and someone buys in city 1, the order will be received in 2 days in this case on Monday (see above array).

如果今天是星期四在城市2有人买,订单将在2天内收到,这里周六。

If today is Thursday and someone buys in the city 2, the order will be received in 2 days, here on Saturday.

根据以上情况,我该怎么用PHP做知道你收到何日产品?

Based on the above, how I can do with PHP to know what day you receive the product?

应该是什么编程逻辑?

推荐答案

没那么容易解释,但

我已经取代了一天的名字与号码,以便我可以计算出下一个。

I've replaced the day names with numbers so I can calculate the next one.

0是星期天,星期一1等。

0 is Sunday, 1 Monday and so on.

function run()
{
    $delivery = 2; // 2 days

    $city1 = array(1, 5); // Monday, Friday
    $city2 = array(1, 4, 6); // Monday, Thursday, Saturday

    // 0 = Sunday

    $today = 4; // Thursday

    $delivery1 = get_delivery_day($city1, $today, $delivery);
    echo "Order in city1 gets delivered $delivery1\n";

    $delivery2 = get_delivery_day($city2, $today, $delivery);
    echo "Order in city2 gets delivered $delivery2\n";
}

function get_delivery_day($city, $today, $delivery)
{
    echo "Trying to deliver\n";
    echo "Today is $today\n";
    echo "city " . implode(',', $city) . "\n";
    // delivery must be zero or lower, and city must deliver today
    // looping before it happens
    while ($delivery > 0 || !in_array($today, $city))
    {
        $today++; // time is increasing by day
        $today %= 7; // if 7 then 0 - looping from Saturday to Sunday
        // remaining days are decreasing
        $delivery--; // can be lower than 0 because it would break >0 condition
        echo "today $today, remaining days $delivery, city " . implode('-', $city) ."\n";
    }

    return $today; // today it will get delivered
}

run();

这篇关于在城市交货天使用阵列()PHP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆