Home  >  Article  >  Backend Development  >  How to convert string to DateTime object using php

How to convert string to DateTime object using php

PHPz
PHPzOriginal
2023-03-21 11:10:371544browse

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 = &#39;2018-03-15&#39;;
$dateTime = DateTime::createFromFormat(&#39;Y-m-d&#39;, $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)
##For example, the example of converting the string "22:30 on March 15, 2018" into a DateTime object is as follows:

<?php
$dateStr = &#39;2018年03月15日 22:30&#39;;
$dateTime = DateTime::createFromFormat(&#39;Y年m月d日 H:i&#39;, $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:

Time zone nameMeaningAfrica /AbidjanCôte d’Ivoire TimeAfrica/AccraGhana Standard Time##Africa/Addis_AbabaAmerica/New_YorkAsia/ShanghaiAustralia/SydneyEurope/ParisPacific/Fiji

例如设置时区为中国标准时间的例子如下:

<?php
$dateStr = &#39;2018-03-15 22:30:15&#39;;
$timezone = new DateTimeZone(&#39;Asia/Shanghai&#39;);
$dateTime = DateTime::createFromFormat(&#39;Y-m-d H:i:s&#39;, $dateStr, $timezone);
echo $dateTime->format('Y-m-d H:i:s');
?>

以上例子将字符串"2018-03-15 22:30:15"转换为DateTime对象,并将时区设置为中国标准时间。

四、总结

通过本文的介绍,我们学习了如何将字符串转换为PHP中的DateTime对象。在实际开发中,我们需要根据时间格式和时区的需求来设置转换参数。同时,在处理时间相关操作时,也需要注意时区的设置以及日期时间格式的转换。

需要指出的是,在字符串转换为DateTime对象过程中,需要保证字符串的格式与时间格式一致。一旦字符串格式不正确,将导致转换失败。因此,在处理时间相关操作时,需要仔细检查字符串格式以及转换参数。

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn