PHP - date formatting. date() function.
Last updated: Sep 23, 2022
Colleagues hello to all.
In today's article we will talk about how you can format the date in the PHP programming language.
When working with your application, website, or database, you will often record the point in time when an event occurs. When writing a date and time to a database, we usually use either the php function time which returns the date as a set of numbers since 1970, or we can use a special type of field in the database called datetime.
The problem is that these date and time options then will not be able to display that date in a human friendly format. In the PHP programming language there is a function that will help you format the date and time in the form you need, and we will look at it today.
The function that will help us format the date today is called date().
date(string $format, ?int $timestamp = null) return string
The function takes one required parameter, this is the template for formatting. The second parameter is optional, it's a timestamp timestamp. If the second parameter is not passed, then the function will use the current Unix system time.
The function returns a formatted date string. Passing a non-numeric value as the timestamp parameter will return false and raise an E_WARNING level error.
Accepted formats, templates.
- d - Day of the month, 2 digits with a leading zero from 01 to 31
- D - Text representation of the day of the week, 3 characters from Mon to Sun
- j - Day of the month without leading zero from 1 to 31
- l - Full name of the day of the week from Sunday to Saturday
- N - Number of the day of the week according to ISO 8601 from 1 (Monday) to 7 (Sunday)
- S - English suffix for the ordinal day of the month, 2 characters st, nd, rd or th. Used in conjunction with j
- w - Number of the day of the week from 0 (Sunday) to 6 (Saturday)
- z - Ordinal number of the day in the year (starting from 0) From 0 to 365
- W - Ordinal number of the week of the year in accordance with the ISO 8601 standard; weeks start on Monday For example: 42
- F - Full name of the month, such as January or March from January to December
- m - Number of the month with a leading zero from 01 to 12
- M - Abbreviated name of the month, 3 characters from Jan to Dec
- n - Number of the month without a leading zero from 1 to 12
- t - The number of days in the specified month from 28 to 31
- L - Leap year flag 1 if the year is a leap year, otherwise 0.
- o - The ISO 8601 year number. Has the same meaning as Y except when the ISO week number (W) belongs to the previous or next year; then the year of that week will be used. Examples: 1999 or 2003
- X - Expanded full numeric representation of the year, at least 4 digits, with - for years BC and + for years AD. Examples: -0055, +0787, +1999, +10191
- x - The extended full numeric representation if required, or the standard full numeric representation if available (for example, Y). At least four digits. Years BC are prefixed with -. Years after (and including) 10000 are prefixed with +. Examples: -0055, 0787, 1999, +10191
- Y - Full numeric representation of the year, at least 4 digits, with - for years BC. Examples: -0055, 0787, 1999, 2003, 10191.
- y - Year number, 2 digits Examples: 99, 03
- a - Ante meridiem (Latin for "before noon") or Post meridiem (Latin for "after noon") in lowercase am or pm
- A - Ante meridiem or Post meridiem in uppercase AM or PM
- B - Internet time (an alternative time-of-day reference system) time from 000 to 999
- g - Hours in 12-hour format without leading zero from 1 to 12
- G - Hours in 24-hour format without leading zero from 0 to 23
- h - Hours in 12-hour format with leading zeros from 01 to 12
- H - Hours in 24-hour format with a leading zero from 00 to 23
- i - Minutes with leading zero from 00 to 59
- s - Seconds with leading zero from 00 to 59
- u - Microseconds. Note that date() will always return 000000 because it takes an integer (int) parameter, while DateTime::format() supports microseconds if the DateTime is created with them. For example: 654321
- v - Milliseconds. The remark is the same as for u. Example: 654
- e - Time zone identifier Examples: UTC, GMT, Atlantic/Azores
- I - (capital i) DST flag 1 if the date is DST, 0 otherwise.
- O - Difference to GMT without colon between hours and minutes Example: +0200
- P - GMT offset with colon between hours and minutes Example: +02:00
- p - Same as P, but returns Z instead of +00:00 (available since PHP 8.0.0) For example: +02:00
- T - Time zone abbreviation, if known; otherwise, GMT offset. Examples: EST, MDT, +05
- Z - Time zone offset in seconds. For time zones located west of UTC, negative numbers are returned, and for those located east of UTC, positive numbers are returned. -43200 to 50400
- c - Date in ISO 8601 format 2004-02-12T15:19:21+00:00
- r - Date in » RFC 222/" RFC 5322 For example: Thu, 21 Dec 2000 16:01:07 +0200
- U - Number of seconds since Unix Epoch (January 1, 1970 00:00:00 GMT) See also time()
Examples of robotic functions.
$datetime = date("F j, Y, g:i a"); | return September 23, 2022, 10:54 pm
$datetime = date("m.d.y"); | return 09.23.22
$datetime = date("j, n, Y"); | return 23, 9, 2022
$datetime = date("Ymd"); | return 20220923
$datetime = date('h-i-s, j-m-y, it is w Day'); | return 10-55-52, 23-09-22, 5530 5552 5 Fripm22
$datetime = date('\i\t \i\s \t\h\e jS \d\a\y.'); | return it is the 23rd day.
$datetime = date("D M j G:i:s T Y"); | return Fri Sep 23 22:56:23 MSK 2022
$datetime = date('H:m:s \m \i\s\ \m\o\n\t\h'); | return 22:09:37 m is month
$datetime = date("H:i:s"); | return 22:56:50
$datetime = date("Y-m-d H:i:s"); | return 2022-09-23 22:57:03
Thank you all, I hope that my article helped you in some way.