PHP Date Function

Title: PHP Date Function
Author: Sazzad Hossain
Language: PHP
Summary: Many people began sending me pm’s on how to do certain things in php. Instead of answering all of them, I thought why not create a thread where people can post the basic functions of php. Here I am talking about a very important php function: php date. I use this function for many purpose, but here I’ll just explain the general purpose/use.

php date() – the PHP date() function formats a timestamp to a more readable date and time. The first parameter in the date() function specifies how to format the date/time. It uses letters to represent date and time formats. Some of these formats include:

Important Full Date and Time:

* r: Displays the full date, time and timezone offset. It is equivalent to manually entering date(“D, d M Y H:i:s O”)

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


I got the list of characters from http://www.tizag.com/
For the complete list click here

So basically all you have to do is call the date() function. You can accomplish this by simply using the following call-out after your <body tag or wherever you wish for it to appear.

<?php
echo date("ITEMHERE");
?>


Replace ITEMHERE with your designated character from the quote box above. If you want to display a normal date enter the following

<?php
echo date("M. d, Y");
?>

Output for this code is the following: Dec. 15, 2007

Bookmark and Share
This entry was posted on Monday, April 20th, 2009 at 1:18 am and is filed under Tutorials. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

Leave a Reply

You must be logged in to post a comment.