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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

SublimeText3 Mac version
God-level code editing software (SublimeText3)

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

Notepad++7.3.1
Easy-to-use and free code editor

WebStorm Mac version
Useful JavaScript development tools
