Home  >  Article  >  Backend Development  >  How to convert Chinese date to timestamp in php

How to convert Chinese date to timestamp in php

藏色散人
藏色散人Original
2021-05-14 09:33:472417browse

php Method to convert Chinese date to timestamp: First create a PHP sample file; then use "date_parse_from_format" and "mktime function" to convert the Chinese year, month and day date into a timestamp.

How to convert Chinese date to timestamp in php

The operating environment of this article: Windows 7 system, PHP version 7.1, Dell G3 computer.

php processes the date containing Chinese year, month and day and converts it to a timestamp (for example, November 08, 2017 to a timestamp)

<?php 
	$str = &#39;2017年11月08号&#39;;
	$arr = date_parse_from_format(&#39;Y年m月d日&#39;,$str);
	$time = mktime(0,0,0,$arr[&#39;month&#39;],$arr[&#39;day&#39;],$arr[&#39;year&#39;]);
	print_r($arr);
	echo &#39;2017年11月08号对应时间戳为:&#39;.$time;
?>

The result is

Array(
  [year] => 2017
  [month] => 11
  [day] => 8
  [hour] => 
  [minute] => 
  [second] => 
  [fraction] =>
  [warning_count] => 0
  [warnings] => Array
  ( 
  ) 
  [error_count] => 0
  [errors] => Array
  (
  )
   
  [is_localtime] =>
  
)

2017 The corresponding timestamp of November 8th is: 1510070400

date_parse_from_formatDefinition and usage

date_parse_from_format() function returns the association containing the specified date information according to the specified format array.

Syntax

date_parse_from_format(format,date);
Parameters Description
format Required. Specifies the format (formats accepted by date_create_from_format()).
date Required. Specify date as a string value.

ktime()定义和用法

gmmktime() 函数返回日期的 UNIX 时间戳。

提示:该函数与 gmmktime() 相同,不同的是传递的参数代表了日期(而不是 GMT 日期)。

语法

mktime(hour,minute,second,month,day,year,is_dst);

year 可选。规定年。

参数 描述
hour 可选。规定小时。
minute 可选。规定分。
second 可选。规定秒。
month 可选。规定月。
day 可选。规定天。


is_dst

可选。如果时间在夏令时 (DST) 期间,则设置为 1,否则设置为 0,若未知则设置为 -1(默认)。

如果未知,PHP 会自己进行查找(可能产生意外的结果)。

注意:该参数在 PHP 5.1.0 中被废弃。取而代之使用的是新的时区处理特性。

<?php
echo(date("M-d-Y",mktime(0,0,0,12,36,2001)));
echo(date("M-d-Y",mktime(0,0,0,14,1,2001)));
echo(date("M-d-Y",mktime(0,0,0,1,1,2001)));
echo(date("M-d-Y",mktime(0,0,0,1,1,99)));
?>

 输出

Jan-05-2002  
Feb-01-2002  
Jan-01-2001  
Jan-01-1999  
PHP time() 函数  
PHP Date / Time 函数

推荐学习:《PHP视频教程

The above is the detailed content of How to convert Chinese date to timestamp in php. 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