Page 1 of 1
simple date function help
Posted: Wed Dec 10, 2008 3:24 pm
by dodgson
I'm very new at this stuff, so please excuse the mess. I'm trying to get the following code to load different pages based on the date between 2 specific days. For some reason, I'm missing something. Any help would be greatly appreciated. Take Care
<?php
ini_set('display_errors', 1);
error_reporting (E_ALL & ~E_NOTICE);
$day = date("n j");
switch($day)
{
case $day >= '12 9' && $day <= '12 25':
header( 'Location:
http://www.google.com' );
exit();
case $day >= '12 26' && $day <= '12 31':
header( 'Location:
http://www.yahoo.com' );
exit();
case $day >= '1 1' && $day <= '1 31':
header( 'Location:
http://www.apple.com' );
exit();
}
?>
Re: simple date function help
Posted: Wed Dec 10, 2008 3:55 pm
by requinix
When doing comparisons like that you have to do two things:
1. The same-type numbers (month, day) have to have the same number of digits regardless of the number itself. This typically means zero-padding.
2. From left to right the string must contain less to more specific information. Meaning the numbers are arranged year, month, day, hour, minute, second in that order.
So for month and day you need to use 'm' and 'd', and it has to be month before day.
Re: simple date function help
Posted: Wed Dec 10, 2008 4:01 pm
by dodgson
I'm sorry for not understanding. I thought that I had to use "n j" (month, day). I'm sure this is very simple to understand. I'm just trying to get my head wrapped around it. Thanks again for your response.
Re: simple date function help
Posted: Wed Dec 10, 2008 4:59 pm
by requinix
You're comparing the dates using strings. That's perfectly fine, but you have to remember that the comparison is a string comparison.
The number 9 is less than the number 10, but the string "9" is more than the string "10". That's because string comparisons look character-by-character: "9" is more than "1".
That's why rule #1 exists. With the zero-padding (could be spaces) you're comparing "09" with "10" - "0" is less than "1".
Rule #2 is a bit harder to explain. Let's say date A is 2007-12-31 and date B is 2008-11-27. If you compare using the days first you'll think B < A. If you compare months you'll reach the same conclusion. It's only when you look at the year first will you see that it's actually A < B.
Re: simple date function help
Posted: Thu Dec 11, 2008 8:10 am
by dodgson
I can't thank you enough for taking the time to explain that to me. I have tested my file based on your suggestions and it's working perfectly.
Thanks again.
