#47: Formatting Dates and Times

Because humans don't understand timestamps very well, whenever you want to print a date, you should use a format such as Oct 15, 2008.

We already touched on the date() function earlier in this chapter; now let's look at how it works. Its two parameters are a date format (such as 'M j, Y') and a timestamp, and the return value is a string. For example, date('M j, Y', 1151884800) yields Jul 2, 2006 in Pacific Standard Time. If you omit the timestamp parameter, date() uses the current time.

The date format can have extra characters, such as colons and commas, but unlike other functions that take a format—such as printf()—you must be especially careful not to use reserved formatting characters because there is no ...

Get Wicked Cool PHP now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.