Home > Article > Backend Development > How to convert date to timestamp in php
When writing programs in PHP, we often need to convert dates to timestamps, or timestamps to dates. This process is very simple in PHP and can be easily accomplished using just a few simple functions. In this article, we'll cover how to convert a PHP date to a timestamp, while also discussing some common usage scenarios.
1. How to convert PHP date to timestamp
To convert PHP date to timestamp, you can use PHP's built-in time() function. The time() function returns the timestamp of the current time. If you want to convert a specified date into a timestamp, you can use the strtotime() function. The strtotime() function accepts a string parameter that contains date and time information and converts it into a timestamp. Here are some examples:
// 获得当前时间戳 $timestamp = time(); echo $timestamp; // 将指定日期转换为时间戳 $date = '2022-06-01'; $timestamp = strtotime($date); echo $timestamp; // 将指定日期和时间转换为时间戳 $datetime = '2022-06-01 10:30:00'; $timestamp = strtotime($datetime); echo $timestamp;
2. Common usage scenarios
Will A common use case for converting dates to timestamps is to store dates in a database. Compared with storing strings in date format, storing timestamps can improve query efficiency. When storing data, you can use the strtotime() function to convert the date into a timestamp, and then store the timestamp in the database.
// 将日期转换为时间戳,并存入数据库 $date = '2022-06-01'; $timestamp = strtotime($date); $sql = "INSERT INTO table_name (timestamp_column) VALUES ($timestamp)";
In addition to storing the date, we also need to display the date on the website. There are functions provided in PHP that can convert timestamps into date strings in various formats.
// 将时间戳转换为指定格式的日期字符串 $timestamp = time(); $date_string = date('Y-m-d H:i:s', $timestamp); echo $date_string; // 输出:2022-06-01 10:30:00
When comparing and calculating time, it is more convenient to use timestamps than dates. You can use the time() function to get the timestamp of the current time, or use the strtotime() function to convert a specified date or time into a timestamp.
// 比较两个时间戳的大小 $timestamp1 = time(); $timestamp2 = strtotime('2022-06-01'); if ($timestamp1 > $timestamp2) { echo 'timestamp1 is later than timestamp2'; } else { echo 'timestamp1 is earlier than timestamp2'; } // 计算两个时间戳之间的时间差 $timestamp1 = time(); $timestamp2 = strtotime('2022-06-01'); $time_diff = $timestamp2 - $timestamp1; echo $time_diff; // 输出:2293200
3. Summary
Converting PHP date to timestamp is one of the common operations when writing PHP programs. The time() function and strtotime() function are provided in PHP, which can easily complete the conversion between dates and timestamps. By converting dates into timestamps, we can easily compare and calculate time in the program, and we can also improve query efficiency when storing date data.
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!