Home >Backend Development >PHP Problem >How to convert characters to time format using PHP

How to convert characters to time format using PHP

PHPz
PHPzOriginal
2023-04-12 13:57:001339browse

In the process of web development, we often need to process time-related data. As a popular web development language, PHP provides a wealth of date and time functions to facilitate our time processing. This article will introduce how to use PHP to convert characters into time format.

1. strtotime() function

PHP’s built-in strtotime() function can convert a character date and time into a Unix timestamp (distance from 00:00:00 on January 1, 1970 seconds). The strtotime() function has two parameters: the first parameter is the date and time string that needs to be converted, and the second parameter is optional and defaults to the current time.

Example:

$date1 = strtotime("2018-12-22"); // 将字符串“2018-12-22”转换为Unix时间戳
$date2 = strtotime("2018-12-22 18:30:00"); // 将字符串“2018-12-22 18:30:00”转换为Unix时间戳

In the above code, $date1 and $date2 are assigned the values ​​of midnight on December 22, 2018 and 6:30 pm on December 22, 2018 respectively. stamp. If the second argument is omitted, it defaults to the current time.

2. DateTime() class

In addition to the strtotime() function, we can also use PHP's built-in DateTime() class to convert characters into time format. Instances of the DateTime() class can be used to perform various date and time related operations, such as formatting dates and times, calculating the difference between two dates, etc.

Example:

$dateStr = '2019-10-01';
$date = new DateTime($dateStr); // 创建一个DateTime对象,使用$dateStr初始化日期
echo $date->format('Y-m-d H:i:s'); // 将$date对象格式化为“年-月-日 时:分:秒”格式的字符串并输出

In the above code, $dateStr is assigned the string "2019-10-01", and then used to initialize a DateTime object, which is used for formatting date and output.

3. Time formatting

After converting characters into time format, we may need to format the time into other types of date and time strings. PHP provides various functions and parameters to help us achieve this goal.

Commonly used time formatting syntax is as follows:

Character Description Example
Y Complete year represented by 4 digits 1999
y Year represented by 2 digits 99
m Month represented by digits (with leading zero) 01 ~ 12
n Number of months (no leading zero) 1 ~ 12
d The day of the month (with leading zero) 01 ~ 31
j The day of the month Days (without leading zeros) 1 ~ 31
H Hours expressed in 24-hour format 00 ~ 23
h The number of hours expressed in 12-hour format 01 ~ 12
i The number of minutes represented by numbers (with leading zeros) 00 ~ 59
s The number of seconds represented by numbers ( With leading zeros) 00 ~ 59
A Capitalized morning or afternoon AM or PM
a lowercase am or pm am or pm

Example:

$timestamp = strtotime('2021-06-30 18:30:45');
$date = new DateTime();
$date->setTimestamp($timestamp);
echo $date->format('Y年m月d日 H:i:s');

In the above code, the $date object will output the formatted time, and the output result is "June 30, 2021 18:30:45".

Conclusion

PHP provides multiple methods to convert characters into time format. In practical applications, we can choose different methods according to the actual situation. For example, using the strtotime() function can quickly convert characters into timestamps, and using the DateTime() class makes it easier to flexibly handle time-related calculations and formatting. By mastering these functions and classes, you can complete time-related tasks more efficiently.

The above is the detailed content of How to convert characters to time format 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