+ Reply to Thread
Results 1 to 7 of 7

Thread: php date comparisons

  1. #1
    purpleflame's Avatar
    purpleflame is offline x10Hosting Member purpleflame is an unknown quantity at this point
    Join Date
    Dec 2008
    Posts
    17

    php date comparisons

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

  2. #2
    xPlozion's Avatar
    xPlozion is offline x10 Elder xPlozion is an unknown quantity at this point
    Join Date
    Mar 2008
    Location
    Delaware, USA
    Posts
    872

    Re: php date comparisons

    use date('d', time()), and that should give you the day number (might be wrong, but check http://www.php.net/date for proper syntax) ;)

  3. #3
    gomarc's Avatar
    gomarc is offline x10 Elder gomarc is an unknown quantity at this point
    Join Date
    Oct 2007
    Location
    USA
    Posts
    511

    Re: php date comparisons

    PHP Code:
    <?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";
    }

    ?>

  4. #4
    xav0989's Avatar
    xav0989 is offline Community Public Relation xav0989 is just really nice
    Join Date
    Jul 2008
    Location
    ifk
    Posts
    4,438

    Re: php date comparisons

    Would be better to do it this way:

    PHP Code:
    <?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.
    Xavier L | Community Public Relations Manager (Free Hosting Support)
    █ Yes, my position is too cool to even exist!
    How am I helping? Rate this post by clicking the icon below! (this is even better than "liking" a post)
    Terms of Service | Acceptable Use Policy | x10Hosting Wiki

  5. #5
    purpleflame's Avatar
    purpleflame is offline x10Hosting Member purpleflame is an unknown quantity at this point
    Join Date
    Dec 2008
    Posts
    17

    Re: php date comparisons

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

  6. #6
    xPlozion's Avatar
    xPlozion is offline x10 Elder xPlozion is an unknown quantity at this point
    Join Date
    Mar 2008
    Location
    Delaware, USA
    Posts
    872

    Re: php date comparisons

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

  7. #7
    purpleflame's Avatar
    purpleflame is offline x10Hosting Member purpleflame is an unknown quantity at this point
    Join Date
    Dec 2008
    Posts
    17

    Re: php date comparisons

    Thanks everyone. ^^

+ Reply to Thread

Similar Threads

  1. Ever Been Suspended For Using PHP?
    By dragoneye_xp in forum Off Topic
    Replies: 26
    Last Post: 08-16-2009, 07:17 PM
  2. [PHP] Variables in PHP
    By Bryon in forum Tutorials
    Replies: 15
    Last Post: 01-29-2009, 09:46 AM
  3. Pregunta sobre las Configuraciones PHP
    By Trevelin in forum Soporte
    Replies: 11
    Last Post: 04-28-2008, 01:50 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
x10hosting free hosting for the masses
dedicated servers