0

I am creating a website that allow deliveries only within certain delivery time frames.

Here is an example of exactly what I'm looking for:

FakeCompany delivers on Wednesday and allows customers to place orders between Friday and Tuesday with a cutoff time of 11 PM on Tuesday night.

I need to figure out when the customer logs in if ordering is allowed (between Friday - Tuesday 11 PM). I also need to know how much longer they have to order.

I know the PHP date('N') function that Friday is 5:

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

and Tuesday is 1:

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

These time ranges may change, so I need a simple solution.

Here is what I started with, and now I'm lost on how to do this.

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

I think I need to get the date of the week based on the DAY (Friday) and convert that to this weeks Friday if Friday has not passed or next weeks Friday and do the same with end date.

Then I need to know if today is between those dates / times.

4 Answers 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";
}

See it in action

1
  • Hey @JohnConde his function only will work in some cases. For example, the start day is Friday not Tuesday. So Friday - Tuesday. But this will change from week to week. Another thing is if now = new DateTime(); is Monday then $tuesday is not last Tuesday. I think we are closer to a solution :) Oct 24, 2013 at 18:39
1

Here is a function to check that today is an approved day then if its tuesday also make sure it is before 11pm:

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

for the end day I think you could do it like this:

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

strtotime('Friday') to get friday and add 3 days of 24 hours to it, and it'll be Tuesday 0 am. Then you add 23 hours time to it as it finish at 11pm.

$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
}
0

Here is exactly what I am looking for.

The dates provided may not be this week or even this month, so we need to figure out based on the date what the day of the week was and set the date on this week or next week to same day depending on today (kinda confusing).

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;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.