0

我正在创建一个仅允许在特定交付时间范围内交付的网站。

这是我正在寻找的一个例子:

FakeCompany 在星期三交货,并允许客户在星期五和星期二之间下订单,截止时间是星期二晚上 11 点。

如果允许订购(周五至周二晚上 11 点之间),我需要弄清楚客户何时登录。我还需要知道他们需要订购多长时间。

我知道星期五是 5 的 PHPdate('N')函数:

date('N', strtotime('Friday'));

星期二是 1:

date('N', strtotime('Tuesday'));

这些时间范围可能会改变,所以我需要一个简单的解决方案。

这是我开始的,现在我不知道如何做到这一点。

//Set today and get from database start / end days and end time
$today = (int)date('N');
$startDay = (int)date('N', strtotime('Friday'));
$endDay = (int)date('N', strtotime('Tuesday'));
$endDayTime = '11:00:00';
//If today is before start date
if($today >= $startDay && $today <= $endDay){
    //This works only if the end date is not the following week
    //It also needs to be before end day time!
}

我想我需要根据 DAY(星期五)获取星期几,如果星期五还没有过去,则将其转换为本周星期五或下周星期五,并对结束日期执行相同的操作。

然后我需要知道今天是否在这些日期/时间之间。

4

4 回答 4

4
$now     = new DateTime();
$tuesday = new DateTime('last Tuesday');
$friday  = new DateTime('Friday 11pm');

if ($tuesday < $now && $now < $friday) {
    $interval = $friday->diff($now);
    echo $interval->format('%d day %h hours %i minutes left to order');
}
else {
    echo "you can't order now";
}

看到它在行动

于 2013-10-24T13:18:23.387 回答
1

这是一个检查今天是否是批准的日期的功能,然后如果它的星期二也确保它在晚上 11 点之前:

/*
Acceptable days:

5 - friday
6 - saturday
7 - sunday
1 - monday
2 - tuesday
*/

//Can they order today?
if(in_array(date('N'),array(1,2,5,6,7))){
    //if today is tuesday is it before 11pm?
    if(date('N') == 2){
        if(date('H')<23){
             //23 = 11pm in 24 hour time 
             //Then can order
        }
        else{
             //Then CANT order
        }
    }
    //Its not tuesday so we dont care what time it is they can order
}
于 2013-10-24T03:04:46.710 回答
0

最后一天,我认为您可以这样做:

$endDay = (int)date('N', strtotime('Friday') + 3 *  24 * 3600 + 23 * 3600); 

strtotime('Friday') 获取星期五并添加 3 天 24 小时,这将是星期二早上 0 点。然后,当它在晚上 11 点结束时,您添加 23 小时的时间。

$today = (int)date('N');
$startDay = (int)date('N', strtotime('Friday'));
$endDay = (int)date('N', strtotime('Friday') + 3 *  24 * 3600 + 23 * 3600); 

//If today is before start date
if($today >= $startDay && $today <= $endDay){
    //now it works
}
于 2013-10-24T03:12:14.940 回答
0

这正是我正在寻找的。

提供的日期可能不是本周甚至本月,因此我们需要根据日期确定星期几,并根据今天将日期设置为本周或下周的同一天(有点混乱) .

看到它在行动

//IF USING A DATABASE TO STORE DATE/TIME
//Get route times
//When Using Database: $query = "SELECT * FROM routes WHERE id = '".$user['route_one']."'";
//When Using Database: $result = $this->query($query);
//When Using Database: $route = $this->fetchArray($result);
//Set date vaiables
$thisWeek = new DateTime();
$routeStart = new DateTime(date('Y-m-d H:i:s', strtotime('2013-04-21 00:00:00')));
//When Using Database: $routeStart = new DateTime(date('Y-m-d H:i:s', strtotime($route['start_time'])));
$routeEnd = new DateTime(date('Y-m-d H:i:s', strtotime('2013-04-24 00:00:00')));
//When Using Database: $routeEnd = new DateTime(date('Y-m-d H:i:s', strtotime($route['end_time'])));
$interval = $routeStart->diff($routeEnd);
$numDays = abs($interval->format('%d'));
//Check if today is past or on the start date, else start date is next week, and set day of week
if($thisWeek->format('N') >= $routeStart->format('N')){
    $startDate = $thisWeek->modify('last '.$routeStart->format('l'));
}
else{
    $startDate = $thisWeek->modify($routeStart->format('l'));
}
//Now that we know the start date add the amount of days to the start date to create the end date
$endDate = new DateTime($startDate->format('Y-m-d H:s:i'));
$endDate->modify('+'.$numDays.' days '.$routeEnd->format('H').' hours');
//Check to see if user is within the time range to order or not
$today = new DateTime();
if($startDate <= $today && $today <= $endDate){
    //Find out how much longer ordering can take place
    $interval = $endDate->diff($today);
    $output = 'Allowed to order!<br>';
    $output .= '<div id="orderTimeCounter">'.$interval->format('%d days %h hours %i minutes left to order').'</div>';
}
else{
    //If today is before start date set start date to THIS week otherwise NEXT week
    if($startDate >= $today){
        $startDate = $startDate->modify('this '.$routeStart->format('l'));
    }
    else{
    $startDate = $startDate->modify('next '.$routeStart->format('l'));
}
    //Find out how much longer until ordering is allowed
    $interval = $today->diff($startDate);
    $output = 'Not allowed to order!';
    $output .= '<div id="orderTimeCounter">'.$interval->format('%d days %h hours %i minutes until you can order').'</div>';
}
echo $output;
于 2013-10-31T07:09:00.610 回答