PHP Date/Time Built In Function | South Coast Web Design Ltd
South Coast Web Design Ltd
PHP Date/Time Built In Function Dec 11

source-php-icon

PHP Date Function

the php date function is great. i use it in virtually every website that i write. 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:-

<!--?php 
      $date = date(&amp;quot;Y&amp;quot;) ;&lt;br ?-->
      echo $date ;
?&gt;

So as you can see that will return you :- 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

<!--?php 
         $date = date(&amp;quot;r&amp;quot;);&lt;br ?-->
         echo $date ;
         $date2 = date<em>("D, d M Y H:i:s O")</em>;
         echo $date2;
?&gt;

So that will inturn 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

<!--?php 
     $fileupdated = date('F d Y H:i:s',filemtime('scw_tuts_php3.php'));&lt;br ?-->
     echo $fileupdated ;
?&gt;

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

Simple Aint It??

One Response to “PHP Date/Time Built In Function”

Leave a Response


Top