April 23, 2012
PHP redirect on exact date
Question by user990175
I’m using this code to redirect based on the hour of the day, and the day of the week.
Here it is ..
<?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");
}
?>
I was wondering if there was a way to redirect to a page based on an exact date in the future like April 25th or November 1st.
Thanks .
Answer by Dan Lee
This is a simple approach, which does not take account the year date:
// 25th april:
if (date('d') == '25' && date('m') == '4') {
header('Location: open.php');
}
// 1st nov:
if (date('d') == '1' && date('m') == '11') {
header('Location: open.php');
}
Look at the date() documentation for more exact details.
Answer by Starx
Convert the redirection date into timestamp using strtotime()
or date()
and you can do it easily
if(strtotime("2012/5/3") <= time()) {
header("location: toredirect.php");
exit;
}