Working with dates
Instead of having to manually create a function that displays date information from numbers (like you might have been used to doing back in C++ class) PHP offers a function for this. The function date() will become your best friend when working with time in a PHP project.
date() takes one or two arguments, depending on if you want to display the current date/time or a specific date/time. The first argument is the format string which controls how the outlook will appear. The second (optional) argument is the date that you want to have displayed.
| character | Description | return values |
| Day | --- | --- |
| d | Day of the month, 2 digits with leading zeros | 01 to 31 |
| D | A textual representation of a day, three letters | Mon to Sun |
| j | Day of the month without leading zeros | 1 to 31 |
| l (lower 'L') | Textual representation of the day of the week | Sunday to Saturday |
| N | numeric representation of the day of the week | Mon=1, Sun=7 |
| S | English suffix for the day of the month, 2 letters | st, nd, rd or th. (use with j) |
| z | The day of the year (starting from 0) | 0 to 365 |
| Month | --- | --- |
| F | Textual representation of a month | January to December |
| m | Numeric month, with leading zeros | 01 to 12 |
| M | Short textual representation of a month, three letters | Jan to Dec |
| n | Numeric month, without leading zeros | 1 to 12 |
| Year | --- | --- |
| Y | Year expressed in 4 digits | e.g. 1987 or 2007 |
| y | Year expressed in 2 digits | e.g. 87 or 07 |
| Time | --- | --- |
| a | Lowercase AM / PM | am or pm |
| A | Uppercase AM / PM | AM or PM |
| g | 12-hour format of an hour without zeros | 1 to 12 |
| G | 24-hour format of an hour without zeros | 0 to 23 |
| h | 12-hour format of an hour with zeros | 01 to 12 |
| H | 24-hour format of an hour with zeros | 00 to 23 |
| i | Minutes with zeros | 00 to 59 |
| s | Seconds, with zeros | 00 to 59 |
There are more formatting characters used for the format string of the date function but these are the most common ones. This table was taken from php.net/date.
Let's say that you want to print the current date. What you would do is:
echo date('M t, Y');
?>The result of this function would be Oct 31, 2006. The M is short month, the t is day of month, and the Y is the year. The spaces and comma are printed because they are not formatting characters (One server I used didn't accept 'Y' but instead only took 'o' for the year so the output for the year was a capital Y).
Now, if you want to print a specific date, you would do this:
echo date('M t, Y', $my_date);
?>Now, the format for the specific date is some weird UNIX timestamp, which I believe is in milliseconds from the beginning of UNIX time (being in 1970). This format isn't the same format that you get from, let's say, a MySQL timestamp record. If you take a timestamp from MySQL and put it directly into the second argument for the function, the result will be sometime in the 1970's. This is my solution I am using for this CMS:
echo date("F j, Y g:ia ", strtotime($row['date']));outputs October 29, 2006 1:07am
strtotime() is a native PHP function that takes a wide range of string inputs that represent a date and properly output a UNIX timestamp. The function is quite smart and pretty much any timestamp format you throw at it (HH-MM-SS, YYYY-MM-DD-HH-MM-SS, etc) will work properly.




