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

How to convert date and timestamp in php

PHPz
PHPzOriginal
2023-03-24 16:36:161348browse

PHP is a popular and powerful programming language with a wide range of applications. Among them, the conversion of date and timestamp is also a problem that developers often need to deal with. This article will introduce how to convert dates to timestamps in PHP, and how to convert timestamps to dates.

  1. Convert date to timestamp

In PHP, we can convert date to timestamp through the built-in function strtotime(). The strtotime() function parses a date string into a timestamp and returns false if the parsing fails.

The following is an example:

$dateString = "2021-09-01";
$timestamp = strtotime($dateString);
echo "日期:".$dateString." 转换成时间戳:".$timestamp;

The output result is:

日期:2021-09-01 转换成时间戳:1630483200

Here, we pass the string "2021-09-01" into the strtotime() function , parsing it into a timestamp. As you can see, the converted timestamp is 1630483200, which represents the timestamp of 0:00:00 on September 1, 2021.

It should be noted that the parameter of the strtotime() function is a date string, and its format can be very flexible. For example, the following strings can be correctly parsed as timestamps:

$dateString1 = "20210901";
$dateString2 = "9/1/2021";
$dateString3 = "2021-09-01 09:30:00";
$dateString4 = "next Monday";
  1. Convert timestamps into dates

In PHP, we You can use the date() function to convert a timestamp into a date. The date() function formats the output date using the specified format string.

The following is an example:

$timestamp = 1630483200;
$dateString = date("Y-m-d H:i:s", $timestamp);
echo "时间戳:".$timestamp." 转换成日期:".$dateString;

The output result is:

时间戳:1630483200 转换成日期:2021-09-01 00:00:00

Here, we pass the timestamp 1630483200 into the date() function and format it as The string "Y-m-d H:i:s". As you can see, the converted date is "2021-09-01 00:00:00".

It should be noted that the first parameter of the date() function is a format string, which can specify various parts of the output date. For specific formatting methods, please refer to the PHP manual. At the same time, the second parameter is the timestamp, which represents the number of seconds from 0:00:00 on January 1, 1970 (UTC) to the present.

Summary

This article introduces how to convert dates to timestamps and timestamps to dates in PHP. By using the strtotime() function and date() function, we can quickly solve the problem of date and timestamp conversion. Finally, developers are reminded to pay attention to the influence of factors such as time zone and daylight saving time when processing time to avoid errors.

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