English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
PHP Date & Time Functions Manual
The date_format() function returns a formatted date based on the given format
This function isDateTime :: format()Alias of the function. It accepts a DateTime object and a format string (representing the required date/Time format) as a parameter, set the format of the object in the specified format, and then return the result.
date_format($date_time_object, $format)
Serial number | Parameters and descriptions |
---|---|
1 | date_time_object (required) This is the DateTime object to be formatted. |
2 | format (required) This is a string representing the required format. |
PHP date_format() function returns a formatted date string
This function was originally in PHP version5.2.1introduced in and available in all higher versions.
Here is an example to try, we are creating a DateTime object and formatting it-
<?php //create a DateTime object $date_time_Obj = date_create("25-09-1989"); //format date/time object $format = date_format($date_time_Obj, "y"-d-m"); print("yy-dd-mm format: " ?>Test to see‹/›
Output Result
yy-dd-mm format: 89-25-09
The following example sets the format of the DateTime object to date and time separately-
<?php $dateString = '11-06-2012 12:50:41 GMT'; $dateTimeObj = date_create($dateString); $date = date_format($dateTimeObj, 'd-m-y'); print("Date: " . $date); print("\n"); $time = date_format($dateTimeObj, 'H:i:s'); print("Time: " . $time); ?>Test to see‹/›
Output Result
Date: 11-06-12 Time: 12:50:41
The following example demonstrates several valid format strings in PHP-
<?php $dateTimeObj = date_create("11-06-2019 12:50:41 GMT"); print("y-m-d format: " . date_format($dateTimeObj, 'Y-m-d')); print("\n"); print("d/m/y format: " . date_format($dateTimeObj, 'd/m/y')); print("\n"); print("Y-m-d H:i:s format: " . date_format($dateTimeObj, 'Y-m-d H:i:s')); print("\n"); print("Date in G:i:A format: " . date_format($dateTimeObj, 'G-i-A')); ?>Test to see‹/›
Output Result
y-m-Date in d format: 2019-06-11 d/m/Date in y format: 11/06/19 Y-m-Date in d H:i:s format: 2019-06-11 12:50:41 Date in G:i:A format: 12-50-PM
The following example uses the date_format() method to create a new date-
<?php $dateSrc = '2015-04-19 12:50 GMT'; $dateTime = date_create($dateSrc);; # Now use date_format() to set a new date; date_format($dateTime, "2000-12-12"); echo "The formatted date is " . $dateTime--m-format("Y" echo "<br>"; # Use the second function. $dateTime = new DateTime($dateSrc); $dateTime->setDate( 1999, 10, 12); echo "The formatted date is " . $dateTime--m-format("Y" ?>Test to see‹/›
This produces the following output-
New formatted date for 2005-04-19T12:50:00Z New formatted date for 1999-10-12T12:50:00Z
Some characters have predefined meanings, you can use them to create format strings, they are:
a - "am" or "pm"
A - "AM" or "PM"
d - Day, two digits, leading zeros if less than two digits; e.g., "01" to "31"
D - Day of the week, three letters; e.g., "Fri"
F - Month, full English name; e.g., "January"
h - 12 24-hour format hour; e.g., "01" to "12"
H - 24 12-hour format hour; e.g., "00" to "23"
g - 12 24-hour format hour, no leading zeros if less than two digits; e.g., "1" to "12"
G - 24 24-hour format hour, no leading zeros if less than two digits; e.g., "0" to "23"
i - Minutes; e.g., "00" to "59"
j - Day, two digits, no leading zeros if less than two digits; e.g., "1" to "31"
l - Day of the week, full English name; e.g., "Friday"
m - Month, two digits, leading zeros if less than two digits; e.g., "01" to "12"
n - Month, two digits, no leading zeros if less than two digits; e.g., "1" to "12"
M - Month, three English letters; e.g., "Jan"
s - Seconds; e.g., "00" to "59"
S - Add English ordinal suffix, two English letters; e.g., "th", "nd"
t - Number of days in the specified month; e.g., "28" to "31"
w - Numeric representation of the day of the week, e.g., "0" (Sunday) to "6" (Saturday)
Y - Year, four digits; e.g., "1999"
y - Year, two digits; e.g., "99"
z - Day of the year; e.g., "0" to "365"
U - Total seconds since the Unix epoch (Greenwich Mean Time1970 year1Month1Day 00:00:00)
e - Time zone identifier (for example: UTC, Atlantic/Azores)
I - (uppercase i)-Whether the date is daylight saving time (if it is daylight saving time, then1, otherwise 0)
O - The difference in hours from Greenwich Mean Time (GMT)+0100)
T - PHP machine's time zone setting (for example: EST, MDT)
Z - Time zone offset in seconds. UTC west of the offset is negative, UTC east of the offset is positive (-43200 to43200)
c - ISO-8601Date (for example2004-02-12T15:19:21 + 00:00)
r - RFC 2822Formatted date (for example, Thu,20000 year12Month21Day16:01:07 +0200)