2013-10-24 153 views
0

,我創建了一個網站,只允許一定範圍內交付時間框架交付。指定的日期/時間範圍內的當前日期PHP

下面是一個例子正是我在尋找:

FakeCompany提供在週三和允許客戶將週五和週二之間的訂單,11點的週二晚截止時間。

我需要弄清楚客戶登錄時是否允許訂購(週五至週二晚上11點之間)。我還需要知道他們需要多長時間訂購。

我知道PHP date('N')函數,週五爲5:

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! 
} 

我想我需要獲得(星期五)根據當天的一週的日期,轉換至該週週五,如果週五沒有通過或下週週五和做與結束日期相同。

然後,我需要知道,如果今天的日期/時間之間。

回答

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

+0

嘿@JohnConde 他的功能只會在某些情況下工作。 例如,開始一天是週五不周二。所以星期五 - 星期二。但是這將會逐周改變。 另一件事是如果'now = new DateTime();'是星期一,那麼'$ tuesday'不是'last Tuesday'。 我認爲我們更接近解決方案:) –

相關問題