Reputation: 15
I'm trying to have a page redirect to another page based on the time and date. I'm looking to have it switch over on Wednesday between 7pm & 8pm, and then on Sunday from 1030am to 1130am.
I was able to get it to work, although sending it back from the 2nd page to the first is the issue I'm running into.
Here is the code that i'm using
<?php
$hour = date('G');
$minute = date('i');
$day = date('w');
$m = $hour * 60 + $minute; // Minutes since midnight.
if(
$day == 7 // Sunday...
&& $m >= 615 // ... after 10:15…
&& $m <= 700 // ... but before 11:40…
) header("Location: open.php");
else if(
$day == 3 // Wednesday...
&& $m >= 1125 // ... after 18:45…
&& $m <= 1235 // ... but before 20:35…
) header("Location: open.php");
I've tried switching the numbers around, but I either had an error, or it didn't redirect.
Upvotes: 1
Views: 2352
Reputation: 394
Its best to do this in combination with javascript. The problem is that PHP works server side. So at the time you received a php webpage your php code is already compiled.
In javascript you can reach the answer by using the function set Time out
setTimeout(function, milliseconds);
You can calculate the time by calculating the difference between the current date and the date you want to have the redirect run.
to redirect in javascript use the following function
// similar behavior as an HTTP redirect
window.location.replace("http://stackoverflow.com");
// similar behavior as clicking on a link
window.location.href = "http://stackoverflow.com";
Hoped this helped you further in the quest of looking for your answer.
Upvotes: 0
Reputation: 2327
Here are a few things I would try:
First, Sunday == 0, not 7. Saturday == 6. and everything else between them as such.
Set a timezone just incase you have a default set somewhere else that makes neither statement true.
Use date_default_timezone_set() at the top of your code. I am in central time, so I would set
date_default_timezone_set('America/Chicago');
The default can be changed in php.ini at the line "date.timezone = America/Chicago" my default before I changed it was somewhere in the UK.
I see in a comment that you added.
if($day == 7 && $m >= 700 && $day == 3 1125)
formatted correctly, with sunday as 0.
if($day == 0 && $m >= 700 && $day == 3 && $m <= 1125)
I actually gave you a bad answer on that one at first, but I see what you're trying to do. The problem is that it will only be true from Sunday to wednesday from 700 - 1125 each day. So on Monday at 630 it would be false. The if statement isn't formatted correctly for what you want to do, but you would want to use a timestamp in your condition.
To answer your original code. Try this:
<?php
date_default_timezone_set('/*ENTER YOUR TIME ZONE HERE*/ /*Find it here: http://www.php.net/manual/en/timezones.php*/');
$hour = date('G');
$minute = date('i');
$day = date('w');
$m = $hour * 60 + $minute; // Minutes since midnight.
if(
$day == 0 // Sunday...
&& $m >= 615 // ... after 10:15…
&& $m <= 700 // ... but before 11:40…
) header("Location: open.php");
else if(
$day == 3 // Wednesday...
&& $m >= 1125 // ... after 18:45…
&& $m <= 1235 // ... but before 20:35…
) header("Location: open.php");
Hopefully that solves your problem!
Upvotes: 0