Home > Article > Backend Development > How to convert year, month and day to timestamp in php
Method for converting year, month and day to timestamp: 1. Use the date_parse_from_format() function to process a given date. An associative array containing the specified date information will be returned according to the specified format. The syntax is "date_parse_from_format('Y.m.d ', date)"; 2. Use the "mktime(0,0,0,$arr['month'],$arr['day'],$arr['year']);" statement to convert the year, month and day Just a timestamp.
The operating environment of this article: windows7 system, PHP8 version, DELL G3 computer
PHP specified date to timestamp
Use date_parse_from_format to convert the specified format: For example:
<?php $str = '2018.10.01';//或者 2018年10月1日 $arr = date_parse_from_format('Y.m.d',$str);//如果是2018年10月1日,那么这里就是 Y年m月d日 $time = mktime(0,0,0,$arr['month'],$arr['day'],$arr['year']); print_r($arr); echo '对应时间戳为:'.$time;
Output:
Array ( [year] => 2018 [month] => 10 [day] => 1 [hour] => [minute] => [second] => [fraction] => [warning_count] => 0 [warnings] => Array ( ) [error_count] => 0 [errors] => Array ( ) [is_localtime] => ) 对应时间戳为:1538352000
mktime() definition and usage
## The #gmmktime() function returns the UNIX timestamp of a date. Tip: This function is the same as gmmktime(), except that the parameter passed represents a date (instead of a GMT date).Syntax
mktime(hour,minute,second,month,day,year,is_dst);year Optional. Specified year.
Description | |
---|---|
Optional. Specified hours. | |
Optional. prescribed points. | |
Optional. Specifies seconds. | |
Optional. Specified month. | |
Optional. Specify days. | |
Optional. Set to 1 if the time is during Daylight Saving Time (DST), 0 otherwise, or -1 (default) if unknown. If unknown, PHP will do the search itself (which may produce unexpected results). Note: This parameter is deprecated in PHP 5.1.0. Instead, new time zone handling features are used. |
PHP Video Tutorial"]
The above is the detailed content of How to convert year, month and day to timestamp in php. For more information, please follow other related articles on the PHP Chinese website!