Home  >  Article  >  Backend Development  >  How to convert timestamp to date in php

How to convert timestamp to date in php

PHPz
PHPzOriginal
2023-03-21 17:16:378717browse

In programming, time processing is often needed, and timestamp is a common time format. It refers to the seconds that have passed since January 1, 1970 00:00:00 UTC (Coordinated Universal Time) number. However, timestamps are not intuitive to users, so converting timestamps into dates and times better meets user needs. Converting timestamps to dates is an essential skill for PHP developers.

1. Use the date() function

In PHP, the date() function can convert the timestamp into the required date format. The syntax of the date() function is as follows:

date($format, $timestamp);

Among them, $format represents the date format to be generated, for example, "Y-m-d H:i:s" represents year-month-day hour: minute: second. $timestamp represents the timestamp that needs to be converted. The following is an example of conversion to the current date:

date('Y-m-d', time());

In the above code, the time() function returns the current timestamp, and then the date() function converts it into year-month-day format.

2. Use the strtotime() function

In addition to using the time() function to obtain the current timestamp, in many cases we need to obtain the timestamp of the specified time and convert it is the date format. PHP provides the strtotime() function, which can convert date strings into timestamps. The syntax of the strtotime() function is as follows:

strtotime($time, $now);

Among them, $time represents the time string that needs to be converted, such as "2021-05-20 10:30:00". $now represents an optional parameter, indicating the timestamp used to calculate $time, which defaults to the current time. The following is an example of conversion to a specified date:

strtotime('2021-05-20');

In the above code, the strtotime() function converts the string "2021-05-20" into the timestamp of the date.

3. Use the DateTime() class

In addition to using the date() function and strtotime() function, PHP also provides the DateTime() class for date processing and time processing. The DateTime() class is a more object-oriented way of handling time introduced in PHP 5.2.0. The following is an example of using the DateTime() class to convert a timestamp into date format:

$date = new DateTime();
$date->setTimestamp(1621455646);
echo $date->format('Y-m-d H:i:s');

In the above code, a DateTime() instance is first created, and then the setTimestamp() method is used to set the timestamp to 1621455646, and finally use the format() method to convert it into the format of year-month-day hour:minute:second and output it.

Summary:

All three methods can convert the timestamp into the specified date format. The date() function is suitable for simple date formats, while the strtotime() function is suitable for scenarios where time calculation or relative time needs to be calculated. Using the DateTime() class is more flexible and can handle more complex date requirements. Developers can choose a method that suits them based on the actual situation.

The above is the detailed content of How to convert timestamp to date in 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