要求如下: 我们仅在周一,周三和周五提供产品。 如果订单在下午5点之前下达,那么订单将在同一天(如果是MWF)交付,否则订单将在下一个交货日期交付。 订单将分为3部分。第一次交付的第一和第二产品,第二次交付的第3和第4产品以及第3次交付的第5和第6产品。 需要使用PHP
打印当天交付的产品的交货日期 <?php
date_default_timezone_set('Asia/Kolkata');
//Starting date
$date=date('m/d/Y h:i:s a',time());
//echo $date;
for($i=1;$i<=3;$i++)
{
echo "Entered in the loop \n";
$date=calculateDeliveryDate($date);
if($i==1)
{
echo "First delivery date ".$date."\n";
}
else
{
if($i==2)
{
echo "Second delivery date ".$date."\n";
}
else
{
echo "Third delivery date ".$date."\n";
}
}
$date=date_add(strtotime($date),date_interval_create_from_date_string("1 days"));
}
function calculateDeliveryDate($dt)
{
echo "Entered the function \n";
$dayOfTheWeek=date('D',strtotime($dt));
$timeOfTheWeek=date('H',strtotime($dt));
//echo $dayOfTheWeek."\n";
if(($dayOfTheWeek=='Mon' || $dayOfTheWeek=='Wed' || $dayOfTheWeek=='Fri') && $timeOfTheWeek<17)
{
return $dt;
}
else
{
if($dayOfTheWeek=='Mon' || $dayOfTheWeek=='Wed' || $dayOfTheWeek=='Sat')
{
$dt=date_add(strtotime($date),date_interval_create_from_date_string("2 days"));
return $dt;
}
else
{
if($dayOfTheWeek=='Fri')
{
$dt=date_add(strtotime($date),date_interval_create_from_date_string("3 days"));
return $dt;
}
else
{
$dt=date_add(strtotime($date),date_interval_create_from_date_string("1 days"));
return $dt;
}
}
}
}
?>
答案 0 :(得分:0)
这样的事可能吗?祝你好运。
.productLayout {
background: #CCC;
margin-bottom: 10px;
}
.sameDayDelivery {
width: 200px;
background: green;
}
.nextDayDelivery {
width: 200px;
background: crimson;
}
&#13;
<?php
$orderArray = array(
array("name" => "product1", "orderDate" => "2017-10-28 09:00:00"),
array("name" => "product2", "orderDate" => "2017-10-29 12:30:00"),
array("name" => "product3", "orderDate" => "2017-10-31 13:26:00"),
array("name" => "product4", "orderDate" => "2017-11-1 16:49:00"),
array("name" => "product5", "orderDate" => "2017-11-2 17:00:00"),
array("name" => "product6", "orderDate" => "2017-11-3 16:59:00")
);
// LOOP THROUGH DATES
for($i = 0; $i < count($orderArray); $i++){
$orderDate = date_create_from_format("Y-m-d H:i:s", $orderArray[$i]["orderDate"]);
if(CheckTime($orderDate) == true && CheckDay($orderDate)) {
PrintLayout($orderArray[$i]["name"], $orderDate, $orderDate, true);
} else {
PrintLayout($orderArray[$i]["name"], $orderDate, NextDeliverDate($orderDate), false);
}
}
// return true if time is before 5:00PM
function CheckTime($time){
$d = new DateTime(date_format($time, "Y-m-d")); // returns the time
$deadline = date_time_set($d, 16, 59, 59); // returns day of week in text
if($time <= $deadline){
return true;
} else {
return false;
}
}
// return true if Monday, Wednesday, Friday
function CheckDay($time){
$day = date_format($time, 'w');
if($day == 1 || $day == 3 || $day == 5){
return true;
} else {
return false;
}
}
// return day of the week that the order will be delivered
function NextDeliverDate($time){
$date = date_format($time, 'w');
$deliveryTime = date_format($time, 'Y-m-d H:i:s');
if($date != 6){
$d = new DateTime($deliveryTime);
$d -> modify("+1 day");
return $d;
} else {
$d = new DateTime($deliveryTime);
$d -> modify("+2 day");
return $d;
}
}
// Just a simple layout function to print everything easily.
function PrintLayout($productName, $orderDate, $deliveryDate, $sameDay){
if($sameDay == true){
$attentionText = "Same Day";
$attentionClass = "sameDayDelivery";
} else {
$attentionText = "Next Delivery Day";
$attentionClass = "nextDayDelivery";
}
$orderDay = date_format($orderDate, "Y-m-d"); // returns the time
$orderDayText = date_format($orderDate, "l"); // returns day of week in text
$orderTime = date_format($orderDate, "H:i:s"); // returns the time
$deliveryDay = date_format($deliveryDate, "Y-m-d"); // returns the time
$deliveryDayText = date_format($deliveryDate, "l"); // returns day of week in text
$deliveryTime = date_format($deliveryDate, "H:i:s"); // returns the time
echo "<div class='productLayout'>
<h2>Product Name: ".$productName."</h2>
<div class='".$attentionClass."'>".$attentionText."</div>";
echo "Order Date: [".$orderDay."][".$orderTime."][".$orderDayText."]<br>";
echo "Deliver Date: [".$deliveryDay."][".$deliveryTime."][".$deliveryDayText."]";
echo "</div>";
}
echo "<br><hr><br>
The order will be delivered in 3 parts. <br>
1st and 2nd products on 1st delivery<br>
3rd and 4th product on the 2nd delivery<br>
5th and 6th on 3rd delivery.<br>
<br>
Need to print the delivery dates with the products to be delivered on that day using PHP
";
?>
&#13;