php date comparisons

purpleflame

New Member
Messages
17
Reaction score
0
Points
0
I would like to make a script that outputs a different picture based on if the date. If the date is X then I would like to make it one picture. If the date is Y then I would like to make it output a different picture and so on. Also if the date does not match any of the ones I have set then I would like to have the default picture displayed. The only trouble I am having is that I do not know how to use php to compare the current date/time with some predefined one. I tried looking through the php info at http://www.php.net but I only learned how to display the date.

Thanks for the help. ^^
 

gomarc

Member
Messages
516
Reaction score
18
Points
18
PHP:
<?php

$today = date("l"); // l (lowercase 'L')

echo 'Day of the week: ' . $today ."\n <br />";

switch ($today)
{
case Saturday:
  echo "Today is Saturday";
  $image = "Saturday value";
  break;
case Sunday:
  echo "Today is Sunday";
  $image = "Sunday value";
  break;
default:
  echo "Today is not Saturday or Sunday";
  $image = "default value";
}

?>
 

xav0989

Community Public Relation
Community Support
Messages
4,467
Reaction score
95
Points
0
Would be better to do it this way:

PHP:
<?php

$todayRead = date("l"); // l (lowercase 'L')
$todayNum = date("N");

echo 'Today, we are: ' . $todayRead ."\n <br />";

switch ($todayNum)
{
case 1:
  //Monday
  $image = "Monday image.jpg";
  break;
case 2:
  //Tuesday
  $image = "Tuesday image.jpg";
  break;
case 3:
  //Wednesday
  $image = "Wednesday image.jpg";
  break;
case 4:
  //Thursday
  $image = "Thursday image.jpg";
  break;
case 5:
  //Friday
  $image = "Friday image.jpg";
  break;
case 6:
  //Saturday
  $image = "Saturday image.jpg";
  break;
case 7:
  //Sunday
  $image = "Sunday image.jpg";
  break;
default:
 //problem somewhere
 echo "We can't find the date!";
 break;
}

?>

You can also add some code to show specific images on halloween, christmas, new year's eve, new year, etc.
 

purpleflame

New Member
Messages
17
Reaction score
0
Points
0
Why is the second one better? Is it faster?

So I do a separate date() thing for day, month, year, hour?

Thanks for the help. ^^
 

xPlozion

New Member
Messages
868
Reaction score
1
Points
0
The only difference between the two are how they handle the day of the week. One works on the name of the day and the second works on the numeric value of the day of the week: 0=Sunday 6=Saturday...
 
Top