PHP Date time date formatter


The date() function is used to format the time and return a string.

grammar:

String date(string format[, int timestamp])

The parameter format indicates the format of the time format. Possible ways are as follows:

Formatting Description
Y 4-digit year, y is 2 digits, such as 99 or 1999
m A numeric month, preceded by a leading 0, such as 01. N is no leading 0 numeric month
F Month, the full text format, such as January or March
M A three-letter abbreviated month, such as Jan or Mar
d The days of the month, preceded by leading 0s, such as 03. J is the number of days without leader 0
w Days of the week, expressed as numbers, 0 for Sunday
z Days of the year, range 0-366
W The weeks in the year, such as week 32
H 24-hour format with leading 0, h is 12-hour format
G 24-hour format, no leading 0, g corresponds to 12-hour format
i minute format with leading 0
s seconds format with leading 0
A uppercase on the afternoon, such as AM, a lowercase

The optional parameter timestamp represents the timestamp. The default is time(), which is the current timestamp.

We can use date() function provides a rich format to display the required time and date, as in the following examples:

date("Y-m-d", time()); // display format as 2008-12-01
date("Y.m.d", time()); // display format as 2008.12.01
date("M d Y", time()); // display format as Dec 01 2008
date("Y-m-d H: i", time()); // display format as 2008-12-01 12:01

Tip

if your output time and the actual time difference 8 hours(assuming you use the London time zone), please check the php.ini file, do the following settings:

date.timezone = GMT



Copyright 2014-2017 by DeveloperTut.com. All Rights Reserved.
TwitterGoogle+Facebook