Peritus Tutorials

PHP Date Function Hints and Tips – Cheat Sheet, Examples!

by TrentonD on Dec.24, 2010, under Cheat Sheets, PHP, Tutorials


Today we are going to learn how to use the PHP Date function. It really is a simple function, but there are a lot of parameters that can be used which may make it harder for people to catch on to right away. I hope that with this tutorial, it will make the function a little easier to understand by providing visual examples. Lets begin!

Here is what a simple Date Function looks like:

<?php echo date(“F, j Y”); ?>

That will echo something similar to this:

December 24, 2010

F, j, and Y are three of the many format characters you can use to define what you want to display. Below, I have included a chart that shows you what the most-used characters display, with an example.

d 2 Digit numerical representation of the day, with leading zero 01, 15, 31
j Numerical representation of the day, without leading zero 1, 15, 31
S Two digit suffix for the day st, nd, rd, th
l * Full textual representation of the day Monday, Saturday
D Three letter textual representation of the day Mon, Sat
m Numeric representation of the month, with leading zero 01, 06, 12
n Numeric representation of the month, without leading zero 1, 6, 12
F Full textual representation of the month January, June, December
M Three letter textual representation of the month Jan, Jun, Dec
Y Four digit numerical representation of the year 1999, 2006, 2010
y Two digit numerical representation of the year 99, 06, 10

* Lowercase “L”, not an “i”

Below are some examples using the information in the chart above:

Friday, December 24th 2010

<?php echo date(“l, F jS Y”); ?>

12-24-2010

<?php echo date(“m-j-Y”); ?>

Please take note that capitalization does matter, and that if you include any other letters, you must escape the characters with a “\”, as seen below.

December the 24th

<?php echo date(“F \\t\h\e jS”) ?>

And there you have it! A quick, simple tutorial on how the php date function works. Feel free to use what I have provided as a stepping stone, but make sure you experiment and test out other format characters as well! As always, thank you for reading and feel free to post any comments or questions :)

:, , , , , , , , , , , , , , , , ,

Leave a Reply

Looking for something?

Use the form below to search the site:

Still not finding what you're looking for? Drop a comment on a post or contact us so we can take care of it!

Archives

All entries, chronologically...