Freelance Web Developer

Say Hi

Aug 6, 2010

Format Php Date

Everyday i face a lot for queries from readers about Php Date Format. So writing this post just For the record or referenced. In this article i will try to cover all the required Php Date Formats.

date() will return the current server time. gmdate() is same as the date() but will report the equivalent time in Greenwich Mean Time (GMT).

MySQL date format

This is the format to use in MySQL/SQL statements to show dates in string form.

date('Y-m-d H:i:s')

- or, for date only: -


Unix/C date format

This is the date format as reported by unix utilities such as date

date('D M j G:i:s T Y')
Example: Mon Nov 19 19:03:52 EST 2010

HTTP header date format

This is the format to use for HTTP headers such as
Date:, Expires: and Last-Modified:

gmdate('D, d M Y H:i:s \G\M\T')

Context example:

HTTP/1.1 200 OK
Date: Mon, 24 Nov 2010 22:47:33 GMT
Server: Apache/1.3.33 (Darwin) PHP/4.4.7
Last-Modified: Mon, 24 Nov 2010 23:40:02 GMT
Accept-Ranges: bytes

HTTP cookie date format

This format is for the expiration date of HTTP cookies-

gmdate('D, d-M-Y H:i:s \G\M\T')
Context example-
Set-Cookie: RMID=732423sdfs73242; expires=Fri, 31-Dec-2010 23:59:59 GMT;

RSS date format

This date format is used in RSS feeds, in XML elements such as pubDate or lastBuildDate. GMT is not required, and the time zone may be expressed using the either time zone abbreviation ('T') or the offset from GMT ('O').

date('D, d M Y H:i:s T')
- or -
date('D, d M Y H:i:s O')

Atom date format

Getting the time zone specified correctly was tricky for non-GMT dates. Forcing GMT and denoting it as 'Z' will work in all PHP versions.

date('c') (PHP 5)
- or -

Context example:

Tomorrow's Date Format

 $tomorrowmktime(000date('m') , date('d')+1date('Y'));

Yesterday's Date Formate

$yesterdaymktime(000date('m') , date('d')-1date('Y'));date('Y-m-d',$yesterday )

MySQL to PHP Date Formate

$php_date = date(‘m-d-Y’strtotime($mysql_date));

PHP to MySql Date Formate

$mysql_date = date('Y-m-d H:i:s',strtotime($php_date))

strtotime() Magic

This magical function can convert textual human representations of dates/times into Unix timestamps.
This can be very useful when converting encountering non-standards dates.

strtotime('2003-07-30 -1 month');
strtotime('24 September 1972');
strtotime('24-sep-72 8:02pm');

strtotime('1 year');
strtotime('1 year ago');
strtotime('-1 month');

strtotime('+1 week');
strtotime('+1 week 3 days 2 hours 8 seconds');
strtotime('next Thursday');

strtotime('last Monday');

Your turn

If you want to share any good trick about php time format, you can write in the comment section.

Labels: , ,

By :