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

How to convert date to timestamp in php

PHPz
PHPzOriginal
2023-03-21 19:09:151292browse

PHP is a popular programming language used for web application development. In PHP, date and time are very common data types, and timestamp is a very useful tool when dealing with dates and times. In this article, we will explain how to convert date to timestamp.

Date refers to the name of the day, month and year. The timestamp represents the number of seconds that have elapsed since 00:00:00 on January 1, 1970. Performing date to timestamp conversion in PHP is very simple. Dates can be converted to timestamps using the PHP built-in function strtotime().

The following is a code example that uses the strtotime() function to convert a date to a timestamp:

$my_date = "2021-10-15";
$my_timestamp = strtotime($my_date);
echo "日期". $my_date. "对应的时间戳是:". $my_timestamp;

Output result:

日期2021-10-15对应的时间戳是:1634275200

In the above code, we first convert the date character The string "2021-10-15" is stored in the variable $my_date. We then use the strtotime() function to convert $my_date to a timestamp and store the result in the variable $my_timestamp. Finally, use the echo statement to output the results.

In addition to strings containing only dates, the strtotime() function can also accept strings containing date and time. The following is an example of a string containing date and time:

$my_datetime = "2021-10-15 19:10:30";
$my_timestamp = strtotime($my_datetime);
echo "日期时间". $my_datetime. "对应的时间戳是:". $my_timestamp;

Output result:

日期时间2021-10-15 19:10:30对应的时间戳是:1634302230

We can also convert the timestamp into a date string using the PHP built-in function date() This conversion is easily accomplished.

The following is a code example that uses the date() function to convert a timestamp into a date string:

$my_timestamp = 1634275200;
$my_date = date("Y-m-d", $my_timestamp);
echo "时间戳". $my_timestamp. "对应的日期是:". $my_date;

Output result:

时间戳1634275200对应的日期是:2021-10-15

In the above code, we first The timestamp 1634275200 is stored in the variable $my_timestamp. We then use the date() function to convert $my_timestamp to a date string and store the result in the variable $my_date. Finally, use the echo statement to output the results.

To summarize, the strtotime() and date() functions in PHP provide convenient date and time conversion tools. Using these functions, we can easily convert date and time into timestamp and date string, which is very useful for web application development.

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