In PHP, converting a time string to a timestamp is a very common programming task. A timestamp refers to the number of seconds that have elapsed since Unix time (January 1, 1970). It is widely used to handle dates and times because it is a universal format. Although PHP provides many built-in date and time functions, for some special time formats, time conversion is required.
Below we will discuss how to convert a time string to a timestamp in PHP.
The strtotime() function is an important function for processing time in PHP. It can parse a human-readable date string into a Unix timestamp. The following is how to use the strtotime() function to convert a time string into a timestamp:
$time = strtotime("2019-01-01 10:30:00");
Here, the time string "2019-01-01 10:30:00" is passed to the strtotime() function , which then returns a Unix timestamp representing the time represented by that time string. This timestamp can be stored in the variable $time.
In PHP5.2 and above, we can use the DateTime class to convert a time string into a timestamp. This class provides more date and time processing functions, such as date calculation, date formatting, etc. Here's how to convert a time string to a timestamp using the DateTime class:
$dt = new DateTime("2019-01-01 10:30:00"); $time = $dt->getTimestamp();
Here, we first create a DateTime object $dt and pass it the time string as a parameter. Then, we use the getTimestamp() method to get the timestamp. This timestamp can be stored in the variable $time.
If we have a non-standard time string, such as "01/01/2019 10:30", we can use date_parse( ) function converts it to a timestamp. The date_parse() function can parse a date string into an array that contains the various parts of the date and time. Using these parts, we can build a datetime object and then convert it to a Unix timestamp using the getTimestamp() method.
Here is how to convert a time string into a timestamp using the date_parse() function:
$date_parts = date_parse("01/01/2019 10:30"); $date = new DateTime(); $date->setDate($date_parts["year"], $date_parts["month"], $date_parts["day"]); $date->setTime($date_parts["hour"], $date_parts["minute"], $date_parts["second"]); $time = $date->getTimestamp();
Here, we first use the date_parse() function to parse the time string into an associative array $date_parts. We then use these array elements to create a DateTime object $date. Finally, we use the getTimestamp() method to get the Unix timestamp from $date. This timestamp can be stored in the variable $time.
Summary:
This article introduces three methods of converting time strings into timestamps. By using the strtotime() function, the DateTime class and the date_parse() function, we are able to easily handle different time formats and convert them to Unix timestamps. Whichever method you choose, it's important to remember that date and time formats must be parsed correctly, otherwise time errors will result.
The above is the detailed content of How to convert time string to timestamp in php. For more information, please follow other related articles on the PHP Chinese website!