Blackman123
Blackman123

Reputation: 69

How to stop reservations before today

I'm trying to create a reservation system for a games library of some sort. Users should not be allowed to reserve the game for a day before today.

I tried to do this by changing the date chosen by the user for the start of the reservation to a timestamp. Then I would set the date for today, change it to a timestamp and check if the date chosen by the user is less than todays timestamp.

Here is the code:

$timestamp = strtotime($ReservationStart);

$todaystamp = (strtotime('yesterday midnight'));

if ($timestamp < $todaystamp) {
    die("The date you've chosen is before today, please choose a valid date");
}

I thought this would work but it this code only stops reservations for 2 days past and behind rather than yesterday and behind.

Any ideas on how to get it to work for yesterday?

Upvotes: 1

Views: 33

Answers (1)

Albzi
Albzi

Reputation: 15609

What you are looking for is to use midnight rather than yesterday midnight.

$timestamp = strtotime('midnight');

It seems that the strtotime works backwards, so yesterday midnight would be yesterday at 00:00, so it allows all 24 hours of the next day (yesterday) to be allowed to book in.

midnight would go to the current day's midnight (starting at that current date's 00:00.

If you were worried about getting today's midnight, that would be:

$timestamp = strtotime('today midnight');

So it's quite easy to understand once you learn that.

Upvotes: 1

Related Questions