Home >Backend Development >PHP Problem >How to convert string to DateTime object using php
In PHP, the conversion of strings and time is a frequently encountered problem. Especially when dealing with operations related to timestamps, dates, and times, the conversion of strings and times is particularly important. This article will detail how to convert a string to a DateTime object in PHP.
1. Basic operation of converting a string to a DateTime object
In PHP, you can use the createFromFormat() method in the DateTime class to convert a string into a DateTime object. . The function prototype of this method is as follows:
public static DateTime DateTime::createFromFormat ( string $format , string $time [, DateTimeZone $timezone = NULL ] )
Among them, the $format parameter is used to represent the format of the time string, the $time parameter represents the string that needs to be converted into a DateTime object, and the $timezone parameter represents the time zone that needs to be set. . The value of the $format parameter is the same as the strftime() function, as follows:
Format | Meaning |
---|---|
%d | The day of the month with leading zeros (01-31) |
%m | Month, with leading zeros (01-12) |
%Y | Year, four digits |
%H | Hours, 24-hour format, with leading zeros (00-23) |
%i | Minutes, with leading zeros (00-59) |
%s | Number of seconds, with leading zeros (00-59) |
%p | Lowercase letters for morning or afternoon (am or pm) |
%P | Uppercase letters for morning or afternoon (AM or PM) |
The following is an example of converting a string into a DateTime object:
<?php $dateStr = '2018-03-15'; $dateTime = DateTime::createFromFormat('Y-m-d', $dateStr); echo $dateTime->format('Y-m-d H:i:s'); ?>
This example converts the string "2018-03-15" into a DateTime object and uses the format() method Format it into the form of "2018-03-15 00:00:00".
2. String conversion in date and time format
When processing strings in date and time format, we need to use a specific date and time format for conversion. The following are some commonly used date and time formats:
Format | Meaning |
---|---|
Y-m-d | Year, month and day (for example: 2018-03-15) |
Y/m/d | Year, month and day (for example: 2018/03 /15) |
Y.m.d | Year, month and day (such as: 2018.03.15) |
Y year m month d day | Year, month and day (for example: March 15, 2018) |
Ymd | Year, month and day (for example: 20180315) |
H:i:s | Hour: Minute: Second (eg: 22:30:15) |
H:i | Hour: minute (e.g.: 22:30) |
Y-m-d H:i:s | Year, month, day hour: minute: second (e.g.: 2018 -03-15 22:30:15) |
Y year m month d day H:i | year month day hour: minute (for example: March 2018 22:30 on the 15th) |
<?php $dateStr = '2018年03月15日 22:30'; $dateTime = DateTime::createFromFormat('Y年m月d日 H:i', $dateStr); echo $dateTime->format('Y-m-d H:i:s'); ?>This example converts the string "2018-03-15 22:30" into a DateTime object, and uses the format() method to format it into the form of "2018-03-15 22:30:00".
3. Time zone setting
In the date and time processing process, the time zone setting is very important. Time zone settings can be achieved through static methods in the DateTimeZone class. Here are some commonly used time zones:Meaning | |
---|---|
Côte d’Ivoire Time | |
Ghana Standard Time | |
Africa Eastern Time | |
US Eastern Time | |
China Standard Time | |
Australia Eastern Standard Time | |
Central European Time | |
Fiji Time |
The above is the detailed content of How to convert string to DateTime object using php. For more information, please follow other related articles on the PHP Chinese website!