Date Displays with PHP

Just a few minutes ago, a friend asked about displaying a future date on his web site. In this particular example, he wanted the name of the next month. In other words, he’d like the word “October” displayed, since it is currently September. Next month it should change to “November”, and so on.

Now actually entering a word isn’t difficult – but he wanted it to happen automatically. Luckily the page was published using PHP, making it really easy to do with just a small amount of code.

The challenge is coming up with something that isn’t too complex (so that he can maintain it on his own if the need arises), but that will work for him on a regular basis and provide the flexibility required.

Something like this worked wonderfully for his needs, which means that he can get the date to display automatically and not have to update it manually:

  <?php
  $nextmonth = mktime(0, 0, 0, date("m")+1, 1, date("Y"));
  echo date("F", $nextmonth);
  ?>

This could also be made into a PHP function that is called, or it could just be called at the right location to spit out the text form of next month right in that spot.


Posted

in

Comments

5 responses to “Date Displays with PHP”

  1. Chad Everett Avatar

    Heh. That’s what I get for trying to do too many things at once. Thanks for another catch.

  2. Brent Avatar

    I noticed you updated your code… but you replaced ‘date(“d”)’ with ‘date(1)’… not ‘1’. 😛

    -b

  3. Chad Everett Avatar

    Good tip. Thanks!

  4. Brent Avatar

    You’ll actually want to replace the date(“d”) with 1, since on any months with 31 days (ie. Oct), your function will generate a timestamp of 11-31-2005, which of course doesn’t exist, but PHP will try to help you out by returning the timestamp for 12-01-2005, and you’ll get ‘December’

    See Example 1

    -b

  5. Jesse Gardner Avatar

    <grin> Don’t let him fool you ladies and gentlemen. He wrote this little PHP diddy in under a minute. It’s child’s play to him. </grin>