我在两个不同的城市安排了我的网上商店的交货日期:
$city1 = array("Monday", "Friday");
$city2 = array("Monday", "Thursday", "Saturday");
商店每2天发货一次,但仅限于某些日子,具体取决于城市。
如果今天是星期四,有人在1号城市购买,则在本周一的情况下,订单将在2天内收到(见上面的数组)。
如果今天是星期四,有人在2号城购买,订单将在2天内收到,星期六在这里。
基于以上所述,我如何使用PHP来了解您收到产品的那一天?
编程的逻辑应该是什么?
答案 0 :(得分:0)
为了获得接收日,你可以做这样的事情
<?php
$days = array("Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday");
$city1 = array("Monday","Thursday");
$city2 = array("Monday", "Thursday", "Saturday");
$cities = array("city1" => $city1,"city2" => $city2);
//returns the receive day actually the index for $days array
function get_day($city,$order_date){
global $days;
$key = array_keys($days,$order_date);
$key = $key[0];
$receive_day = getnexttwo($key+2,$city);
return $receive_day;
}
//Utility to iterate by 2
function getnexttwo($key,$city){
global $cities,$days;
$next_day_key = $key%7;
if(in_array($days[$next_day_key],$cities[$city])){
return $next_day_key;
}
return getnexttwo($next_day_key+2,$city);
}
$order_day = $days[get_day("city2","Saturday")];
echo $order_day;
答案 1 :(得分:0)
解释起来并不容易,但
我已用数字替换了日期名称,因此我可以计算下一个名称。
0是星期日,1星期一,依此类推。
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();
答案 2 :(得分:0)
如果我理解你的问题,商店需要至少两天才能送货,对吗?
如果是这样,你可以尝试这样的事情:
function guess_delivery_day($order_city, $order_day) {
$delivery_days = array(
//index of the week days are based on date('N'), see http://php.net/manual/en/function.date.php
'city1' => array(1 => "Monday", 5 => "Friday"),
'city2' => array(1 => "Monday", 4 =>"Thursday", 6 => "Saturday")
);
if (isset($delivery_days[$order_city])) {
$guessed_day = $order_day + 2; //Assuming the store take at least two days to ship a product;
if ($guessed_day > 7) {
$guessed_day = $guessed_day - 7; //If bigger then 7, it will be shipped on the next week
}
if (isset($delivery_days[$order_city][$guessed_day])) {
return $delivery_days[$order_city][$guessed_day]; //we got lucky and the guessed day matches a delivery day
}
end($delivery_days[$order_city]); //set the pointer at the end of the array
if ($guessed_day > key($delivery_days[$order_city])) {
reset($delivery_days[$order_city]); //set the pointer at begining of the array
return current($delivery_days[$order_city]); //the delivery day will be the first day available on that city
}
//We search for the following delivery days
for($i = $guessed_day; $i <= 7; $i++) {
if (isset($delivery_days[$order_city][$i])) {
return $delivery_days[$order_city][$i];
}
}
return false; //Something is wrong, it shouldn't be reached
} else {
return false; //Something is wrong with this order;
}
}