Home > Article > Backend Development > How to convert string to date format in php
Method to convert to date format: 1. Use the "strtotime($string)" statement to convert the date and time text of the specified string type into a timestamp; 2. Use "date("Y-m-d H:i The :s",timestamp)" statement formats the timestamp and converts it into the date format of "year-month-day hour:minute:second".

The operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer
php will convert string Implementation method for date format
:
Use strtotime() to extract the time and date from the string, converting the English The date and time description of the text description is parsed into a UNIX timestamp
date() formats the timestamp and converts it to a date in the specified format
Example:
<?php
header('content-type:text/html;charset=utf-8');
ini_set('date.timezone', 'Asia/Shanghai');//设置时区
echo date("Y-m-d H:i:s",strtotime("now")), "<br />";
echo date("Y-m-d H:i:s",strtotime("10 September 2000")), "<br />";
echo date("Y-m-d H:i:s",strtotime("+1 day")), "<br />";
echo date("Y-m-d H:i:s",strtotime("+1 week")), "<br />";
echo date("Y-m-d H:i:s",strtotime("+1 week 2 days 4 hours 2 seconds")), "<br />";
echo date("Y-m-d H:i:s",strtotime("next Thursday")), "<br />";
echo date("Y-m-d H:i:s",strtotime("last Monday")), "<br />";
?>
Description:
date() first parameter formats special characters that can be recognized in the string As shown in the following table:
| Characters | Description | Return value example |
|---|---|---|
| d | in the month The day of the week is represented by two digits. If there are less than two digits, add 0 | 01 to 31 |
| in English for the day of the week. Abbreviation (using 3 letters) | Mon to Sun | |
| English word for day of the week | Sunday to Saturday | |
| According to the ISO-8601 standard format, use numbers to represent the days of the week (newly added in PHP5.1.0) | 1 (meaning Monday) to 7 (meaning Sunday) | |
| The English suffix after the day of the month (use 2 characters to represent ) | st, nd, rd or th. Can be used with j | |
| Use numbers to represent the day of the week | 0 (for Sunday) to 6 (for Saturday) | |
| Use numbers to represent the days of the year | 0 to 365 | |
| According to the ISO-8601 standard format, use numbers to represent the week of the year, each week starts on Monday, (new in PHP 4.1.0) | For example: 42 (current year The 42nd week) | |
| English words for the month, such as January or June | January to December | |
| Use two digits to represent the current month | 01 to 12 | |
| The English abbreviation of the month | Jan to Dec | |
| Use numbers to represent the current month | 1 to 12 | |
| Specify the number of days in the month | 28 to 31 | |
| Whether the specified year is a leap year | If it is a leap year, the value is 1, otherwise it is 0 | |
| Use numbers to represent the year according to the ISO-8601 standard format, which is the same as Y. Same (new in PHP 5.1.0) | 1999 or 2019 | |
| Use 4 digits to represent the complete year | For example: 1999 or 2019 | |
| Use 2 digits to represent the year | For example: 99 or 03 | |
| Lowercase AM and PM values | am or pm | ##A |
| AM or PM | B | |
| 000 to 999 | g | |
| 1 to 12 | G | |
| 0 to 23 | h | |
| 01 to 12 | H | |
| 00 to 23 | i | |
| 00 to 59> | s | |
| 00 to 59> | u | |
| For example: 654321 | e | |
| For example: UTC, GMT, Atlantic/Azores | I (capital "i") | |
| Daylight saving time is 1, otherwise it is 0 | O | |
| For example: 0200 | P | |
| For example: 02:00 | T | |
| For example: EST, MDT (complete text format under windows, such as "Eastern Standard Time" , the Chinese version will display "China Standard Time") | Z | |
| -43200 to 43200 | c | |
| 2014-02-12T15:19:21 00:00 | r | |
| For example: Thu,21 Dec 2000 16:01:07 0200 | U | |
| Returns the same timestamp as time() |
The above is the detailed content of How to convert string to date format in php. For more information, please follow other related articles on the PHP Chinese website!