PHP Date/Time Built In Function

The php date function is great; i use it on virtually every website that I write, for example, the copyright text at the bottom of this page uses the date function to determine which year to stamp on it. The function is called quite simply by using:-


      echo $date ;
?>

So as you can see that will return :- 2009

If you scroll down the page it will show the various letters to put into the date() to give you various different displays.

Example 2,


         echo $date ;
         $date2 = date("D, d M Y H:i:s O");
         echo $date2;
?>

So that will in turn give you : – Fri, 11 Dec 2009 21:58:57 +0000
‘r’ is the full description code for the same as referenced in $date2.

Example 3:


     echo $fileupdated ;
?>

This little line will give you the date that a file was last uploaded to your server. This function I use alot with a non-live pricelist website that can only be updated manually.

This example will give you the following : April 13 2009 22:56:06;

There are so many useful reasons to use the date function; for the best variation that you will need, browse down below to get a list of display types. The best thing to do is to try out a few combinations in a small php file.

Full List Of Date Combinations

Time:

  • a: am or pm depending on the time
  • A: AM or PM depending on the time
  • g: Hour without leading zeroes. Values are 1 through 12.
  • G: Hour in 24-hour format without leading zeroes. Values are 0 through 23.
  • h: Hour with leading zeroes. Values 01 through 12.
  • H: Hour in 24-hour format with leading zeroes. Values 00 through 23.
  • i: Minute with leading zeroes. Values 00 through 59.
  • s: Seconds with leading zeroes. Values 00 through 59.

Day:

  • d: Day of the month with leading zeroes. Values are 01 through 31.
  • j: Day of the month without leading zeroes. Values 1 through 31
  • D: Day of the week abbreviations. Sun through Sat
  • l: Day of the week. Values Sunday through Saturday
  • w: Day of the week without leading zeroes. Values 0 through 6.
  • z: Day of the year without leading zeroes. Values 0 through 365.

Month:

  • m: Month number with leading zeroes. Values 01 through 12
  • n: Month number without leading zeroes. Values 1 through 12
  • M: Abbreviation for the month. Values Jan through Dec
  • F: Normal month representation. Values January through December.
  • t: The number of days in the month. Values 28 through 31.

Year:

  • L: 1 if it’s a leap year and 0 if it isn’t.
  • Y: A four digit year format
  • y: A two digit year format. Values 00 through 99.

Other Formatting:

  • U: The number of seconds since the Unix Epoch (January 1, 1970)
  • O: This represents the Timezone offset, which is the difference from Greenwich Meridian Time (GMT). 100 = 1 hour, -600 = -6 hours
Copyright 2007 - 2024 southcoastweb is a brand of DSM Design.