simple date function help

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
dodgson
Forum Newbie
Posts: 3
Joined: Wed Dec 10, 2008 3:19 pm

simple date function help

Post 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();
}
?>
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: simple date function help

Post 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.

Code: Select all

$day = date("m d");
dodgson
Forum Newbie
Posts: 3
Joined: Wed Dec 10, 2008 3:19 pm

Re: simple date function help

Post 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.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: simple date function help

Post 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.
dodgson
Forum Newbie
Posts: 3
Joined: Wed Dec 10, 2008 3:19 pm

Re: simple date function help

Post 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. :D
Post Reply