Home >Backend Development >PHP Problem >Detailed explanation of how to convert year to timestamp in PHP
In PHP, a timestamp is a ten-digit integer that represents the number of seconds from January 1, 1970 00:00:00 GMT (Greenwich Mean Time) to a certain time. Timestamps are very useful in many scenarios, such as recording creation time, sorting, calculating time differences, etc.
In practical applications, we often encounter the need to convert years into timestamps. At this time, PHP provides several convenient functions to implement this function.
Next, let’s introduce several commonly used PHP functions for converting years into timestamps.
The strtotime() function in PHP can convert any English text date time or formatted date time string into a Unix timestamp. The syntax is as follows:
strtotime ( string $time [, int $now = time() ] ) : int
Among them, the $time
parameter is the date and time string to be converted, and the $now
parameter is the optional current Unix timestamp. The default is current time.
When using this function, you only need to pass a year string that conforms to the format, and you can get its corresponding timestamp, as shown below:
$timestamp = strtotime('2022-01-01 00:00:00'); echo $timestamp;
The above code will output: 1640985600.
If you only need to convert the year, you can remove the month and date parts in the string, as shown below:
$year = "2022"; $timestamp = strtotime($year . '-01-01 00:00:00'); echo $timestamp;
The above code can also get the timestamp of 1640985600.
The mktime() function in PHP is used to return a Unix timestamp representing the number of seconds in the date specified by the parameter. The syntax is as follows:
mktime ( [ int $hour = date("H") [, int $minute = date("i") [, int $second = date("s") [, int $month = date("n") [, int $day = date("j") [, int $year = date("Y") [, int $is_dst = -1 ]]]]]]] ) : int
Among them, $hour
, $minute
, $second
, $month
, ## The #$day and
$year parameters are required and must be passed in order.
$year = "2022"; $timestamp = mktime(0, 0, 0, 1, 1, $year); echo $timestamp;The above code can also get the timestamp of 1640985600. DateTime()PHP provides the DateTime class to handle date and time. Use this class to perform various operations on date and time, including formatting, addition and subtraction, etc. Among them, the
getTimestamp() method can obtain the Unix timestamp.
$year = "2022"; $dateObj = DateTime::createFromFormat('Y', $year); $timestamp = $dateObj->getTimestamp(); echo $timestamp;The above code can also get the timestamp of 1640985600. SummaryConverting year to timestamp is one of the common operations in PHP. We have introduced three implementation methods: strtotime(), mktime() and DateTime class. These methods can all convert years into timestamps, and you need to choose different methods according to the actual scenario.
The above is the detailed content of Detailed explanation of how to convert year to timestamp in PHP. For more information, please follow other related articles on the PHP Chinese website!