Let's talk about php timestamp conversion

PHPz
Release: 2023-03-29 10:22:01
Original
988 people have browsed it

Timestamp conversion php

In website development, timestamps are often used to record the time of events. In practical applications, we need to convert the timestamp into a human-readable format, such as year, month, day, hour, minute and second.

In PHP, you can use the date() function to convert a timestamp into a date string in a specific format. Next we will introduce the method of converting timestamp into date string.

1. Get the current timestamp

In PHP, you can use the time() function to get the current timestamp. The time() function returns the current Unix timestamp, which is the number of seconds since January 1, 1970. The code is as follows:

$now = time();
Copy after login

2. Convert timestamp to date string

We can use the date() function to convert timestamp into a date string in a specified format. The first parameter of the date() function is the date format, and the second parameter is the timestamp to be converted. The following is an example:

$now = time();
$date_string = date('Y-m-d H:i:s', $now);
echo $date_string;
Copy after login

Among them, 'Y-m-d H:i:s' represents the date format, Y represents the four-digit year, m represents the two-digit month, d represents the two-digit day, and H represents 24 The number of hours in the hour measurement method, i represents the number of minutes, and s represents the number of seconds.

3. Commonly used date formats

The following are some common date formats:

Y-m-d H:i:s         2019-01-01 12:00:00
Y年m月d日 H:i:s     2019年01月01日 12:00:00
Y年m月d日           2019年01月01日
Y-m-d               2019-01-01
H:i:s               12:00:00
Copy after login

4. Convert date string to timestamp

If We already have a date string, which we can convert to a timestamp using the strtotime() function. For example:

$date_string = '2019-01-01 12:00:00';
$timestamp = strtotime($date_string);
echo $timestamp;
Copy after login

5. Time zone setting

In PHP, you can use the date_default_timezone_set() function to set the current time zone. If you do not set a time zone, PHP will use the server's default time zone. For example:

// 设置时区为纽约时间
date_default_timezone_set('America/New_York');
Copy after login

6. Time operations based on DateTime class

PHP 5.2.0 introduces the DateTime class, which provides more convenient time operations. The official documentation for this class is https://www.php.net/manual/zh/book.datetime.php. The following is an example:

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

The DateTime class also provides some convenient functions, such as add(), sub(), diff(), etc., which can perform operations such as addition, subtraction, and comparison of time. For example:

$date1 = new DateTime('2020-01-01 12:00:00');
$date2 = new DateTime('2020-01-02 12:00:00');
$interval = $date1->diff($date2);
echo $interval->format('%R%a days');
Copy after login

In the above code, $interval outputs '1 days'.

To sum up, in order to better apply timestamps in PHP development, we need to master the use of timestamp conversion functions and DateTime classes.

The above is the detailed content of Let's talk about php timestamp conversion. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
php
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template