How to convert year, month and day to timestamp in php

藏色散人
Release: 2023-03-08 10:02:01
Original
4006 people have browsed it

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.

How to convert year, month and day to timestamp in php

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 = &#39;2018.10.01&#39;;//或者 2018年10月1日
$arr = date_parse_from_format(&#39;Y.m.d&#39;,$str);//如果是2018年10月1日,那么这里就是 Y年m月d日
$time = mktime(0,0,0,$arr[&#39;month&#39;],$arr[&#39;day&#39;],$arr[&#39;year&#39;]);
print_r($arr);
echo &#39;对应时间戳为:&#39;.$time;
Copy after login

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
Copy after login

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);
Copy after login
year Optional. Specified year.

ParametersDescriptionhourOptional. Specified hours. minuteOptional. prescribed points. secondOptional. Specifies seconds. monthOptional. Specified month. day Optional. Specify days. is_dst
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.

[Recommended learning: "

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!

Related labels:
php
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template