Practical Web Programming

Tuesday, January 06, 2009

PHP: How to Format Date Using date and strtotime Functions

In PHP formatting a date to your desired format is so easy using the date and strtotime functions.

According to the PHP documentation strtotime parses any English textual datetime description into a Unix timestamp. It expects to be given a string containing a US English date format and will try to parse that format into a Unix timestamp (the number of seconds since January 1 1970 00:00:00 UTC)

Syntax:

int strtotime  ( string $time  [, int $now  ] )


Example:

print strtotime("Dec. 25, 2008 10:00 AM");


The date function on the other hand returns a string formatted time/date according to the given format string using the given integer timestamp or the current time if no timestamp is given.

Syntax:

string date  ( string $format  [, int $timestamp  ] )


Example:

print date("M d, Y H:i:s A");


So much for the long explanation. Here's how to format a date using the both functions.

print date("M d, Y H:i:s A", strtotime("Dec. 25, 2008 10:00 AM"));


For the complete list of format for the date functions, visit the PHP documentation here.

2 comments:

Anonymous said...

kuyawa najud nimo wel oi
apply ko sa imo beh...
willing to undergo training nimo...
hehehe....mon2x

kabalweg said...

Mon2x,

Cge, kanus-a man?

Recent Post