Home  >  Article  >  Backend Development  >  How to convert date to timestamp using PHP? 4 methods introduced

How to convert date to timestamp using PHP? 4 methods introduced

PHPz
PHPzOriginal
2023-03-29 16:26:135108browse

PHP is a server-side programming language that is widely used in web development. A timestamp is a numerical representation that represents the number of seconds since 0:00:00 on January 1, 1970 (the starting time of the UNIX timestamp) to the present. In web development, conversion between dates and timestamps is a frequently encountered problem. This article will introduce how to use PHP to convert dates into timestamps.

1. Use the time() function

PHP has a built-in time() function, which returns the current Unix timestamp. If no parameters are passed, the current timestamp is returned. If you want to convert a date into a timestamp, just convert the date into seconds. The following is a sample code:

$date = "2022-05-15";
$timestamp = strtotime($date);
echo $timestamp;

In the above code, $date is a date string, and the strtotime() function converts the string into a Unix timestamp. The last line of code prints the timestamp.

2. Use the strtotime() function

strtotime() is a very useful function that can convert various forms of date strings into Unix timestamps. This function can understand a variety of date string formats, for example:

  • "now": represents the current time
  • "10 September 2000": represents a specific date
  • " 1 day": Indicates adding one day to the current date
  • "next Wednesday": Indicates the date of next Wednesday
  • "last Friday": Indicates the date of last Friday

The following is an example of using the strtotime() function to convert a date into a timestamp:

$date = "2022-05-15";
$timestamp = strtotime($date);
echo $timestamp;

In the above code, $date is a date string, and the strtotime() function will This string is converted to a Unix timestamp. The last line of code prints the timestamp.

3. Use DateTime object

PHP provides a DateTime class, which can help developers manipulate dates and times. This class has many useful methods for getting, setting and manipulating dates, such as format(), add(), sub(), etc. The following is an example of using a DateTime object to convert a date into a timestamp:

$date = "2022-05-15";
$datetime = new DateTime($date);
$timestamp = $datetime->getTimestamp();
echo $timestamp;

In the above code, $date is a date string, and the constructor of the DateTime class will convert the string into a DateTime object. In the third line of code, the getTimestamp() method converts the object into a Unix timestamp. The last line of code prints the timestamp.

4. Use the mktime() function

In addition to the functions and classes introduced above, PHP also provides a mktime() function, which can convert a given date into a Unix timestamp. The parameters of this function are hours, minutes, seconds, months, days, and years. The following is an example of using the mktime() function to convert a date into a timestamp:

$timestamp = mktime(0, 0, 0, 5, 15, 2022);
echo $timestamp;

In the above code, the mktime() function converts 0:00:00 on May 15, 2022 into A Unix timestamp and prints the timestamp.

Summary

This article introduces four methods of using PHP to convert dates into timestamps: using the time() function, strtotime() function, DateTime object and mktime() function. Developers can choose a method that suits them according to their needs. No matter which method is used, the date can be easily converted into a Unix timestamp, making it convenient for developers to process date and time in web applications.

The above is the detailed content of How to convert date to timestamp using PHP? 4 methods introduced. 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