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

Author: Nabin Nepal (Starx)

Hello, I am Nabin Nepal and you can call me Starx. This is my blog where write about my life and my involvements. I am a Software Developer, A Cyclist and a Realist. I hope you will find my blog interesting. Follow me on Google+

...

Please fill the form - I will response as fast as I can!