如何在PHP中将日期yyyyyy - mm - dd转换为epoch [duplicate]

时间:2022-12-01 16:03:47

This question already has an answer here:

这个问题已经有了答案:

As per title: how to convert a string date (YYYY-MM-DD) to epoch (seconds since 01-01-1970) in PHP

如标题所示:如何在PHP中将字符串日期(yyyyyy - mm - dd)转换为epoch(秒从01-01-1970年开始)

4 个解决方案

#1


14  

Perhaps this answers your question

也许这能回答你的问题

http://www.epochconverter.com/programming/functions-php.php

http://www.epochconverter.com/programming/functions-php.php

Here is the content of the link:

以下是链接的内容:

There are many options:

有很多选项:

  1. Using 'strtotime':
  2. 使用“strtotime”:

strtotime parses most English language date texts to epoch/Unix Time.

strtotime将大多数英语日期文本解析为epoch/Unix时间。

echo strtotime("15 November 2012");
// ... or ...
echo strtotime("2012/11/15");
// ... or ...
echo strtotime("+10 days"); // 10 days from now

It's important to check if the conversion was successful:

重要的是要检查转换是否成功:

// PHP 5.1.0 or higher, earlier versions check: strtotime($string)) === -1
if ((strtotime("this is no date")) === false) {
   echo 'failed';
 }

2. Using the DateTime class:

2。使用DateTime类:

The PHP 5 DateTime class is nicer to use:

PHP 5 DateTime类更易于使用:

// object oriented
$date = new DateTime('01/15/2010'); // format: MM/DD/YYYY
echo $date->format('U'); 

// or procedural
$date = date_create('01/15/2010'); 
echo date_format($date, 'U');

The date format 'U' converts the date to a UNIX timestamp.

日期格式“U”将日期转换为UNIX时间戳。

  1. Using 'mktime':
  2. 使用“mktime”:

This version is more of a hassle but works on any PHP version.

这个版本比较麻烦,但适用于任何PHP版本。

// PHP 5.1+ 
date_default_timezone_set('UTC');  // optional 
mktime ( $hour, $minute, $second, $month, $day, $year );

// before PHP 5.1
mktime ( $hour, $minute, $second, $month, $day, $year, $is_dst );
// $is_dst : 1 = daylight savings time (DST), 0 = no DST ,  -1 (default) = auto

// example: generate epoch for Jan 1, 2000 (all PHP versions)
echo mktime(0, 0, 0, 1, 1, 2000); 

#2


9  

Try this :

试试这个:

$date  = '2013-03-13';

$dt   = new DateTime($date);
echo $dt->getTimestamp();

Ref: http://www.php.net/manual/en/datetime.gettimestamp.php

裁判:http://www.php.net/manual/en/datetime.gettimestamp.php

#3


2  

use strtotime() it provides you Unix time stamp starting from 01-01-1970

使用strtotime(),它为您提供从01-01-1970开始的Unix时间戳

#4


2  

Use the strtotime() function:

使用strtotime()函数:

strtotime('2013-03-13');

#1


14  

Perhaps this answers your question

也许这能回答你的问题

http://www.epochconverter.com/programming/functions-php.php

http://www.epochconverter.com/programming/functions-php.php

Here is the content of the link:

以下是链接的内容:

There are many options:

有很多选项:

  1. Using 'strtotime':
  2. 使用“strtotime”:

strtotime parses most English language date texts to epoch/Unix Time.

strtotime将大多数英语日期文本解析为epoch/Unix时间。

echo strtotime("15 November 2012");
// ... or ...
echo strtotime("2012/11/15");
// ... or ...
echo strtotime("+10 days"); // 10 days from now

It's important to check if the conversion was successful:

重要的是要检查转换是否成功:

// PHP 5.1.0 or higher, earlier versions check: strtotime($string)) === -1
if ((strtotime("this is no date")) === false) {
   echo 'failed';
 }

2. Using the DateTime class:

2。使用DateTime类:

The PHP 5 DateTime class is nicer to use:

PHP 5 DateTime类更易于使用:

// object oriented
$date = new DateTime('01/15/2010'); // format: MM/DD/YYYY
echo $date->format('U'); 

// or procedural
$date = date_create('01/15/2010'); 
echo date_format($date, 'U');

The date format 'U' converts the date to a UNIX timestamp.

日期格式“U”将日期转换为UNIX时间戳。

  1. Using 'mktime':
  2. 使用“mktime”:

This version is more of a hassle but works on any PHP version.

这个版本比较麻烦,但适用于任何PHP版本。

// PHP 5.1+ 
date_default_timezone_set('UTC');  // optional 
mktime ( $hour, $minute, $second, $month, $day, $year );

// before PHP 5.1
mktime ( $hour, $minute, $second, $month, $day, $year, $is_dst );
// $is_dst : 1 = daylight savings time (DST), 0 = no DST ,  -1 (default) = auto

// example: generate epoch for Jan 1, 2000 (all PHP versions)
echo mktime(0, 0, 0, 1, 1, 2000); 

#2


9  

Try this :

试试这个:

$date  = '2013-03-13';

$dt   = new DateTime($date);
echo $dt->getTimestamp();

Ref: http://www.php.net/manual/en/datetime.gettimestamp.php

裁判:http://www.php.net/manual/en/datetime.gettimestamp.php

#3


2  

use strtotime() it provides you Unix time stamp starting from 01-01-1970

使用strtotime(),它为您提供从01-01-1970开始的Unix时间戳

#4


2  

Use the strtotime() function:

使用strtotime()函数:

strtotime('2013-03-13');