当前日期是否在PHP中指定的日期/时间范围内

时间:2013-10-24 02:57:33

标签: php date time

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

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

FakeCompany周三发布,允许客户在周五至周二下订单,截止时间为周二晚上11点。

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

我知道星期五是5的PHP date('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 个答案:

答案 0 :(得分:3)

$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";
}

See it in action

答案 1 :(得分: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
}

答案 2 :(得分:0)

结束那天我认为你可以这样做:

$endDay = (int)date('N', strtotime('Friday') + 3 *  24 * 3600 + 23 * 3600); 
strtotime('星期五')到星期五,加上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
}

答案 3 :(得分:0)

这正是我要找的。

提供的日期可能不是本周甚至是本月,所以我们需要根据日期确定一周中的哪一天,并根据今天将当天或下周的日期设置为当天(有点混乱)。

See It In Action

//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;