如何从例如22-09-2008中获取时间戳?


当前回答

<?php echo date('M j Y g:i A', strtotime('2013-11-15 13:01:02')); ?>

http://php.net/manual/en/function.date.php

其他回答

还有strptime(),它只需要一种格式:

$a = strptime('22-09-2008', '%d-%m-%Y');
$timestamp = mktime(0, 0, 0, $a['tm_mon']+1, $a['tm_mday'], $a['tm_year']+1900);

警告:

Windows系统不支持该功能 该函数在PHP 8.1.0中已弃用。非常不鼓励依赖这个函数。

如果你想确定一个日期是否被解析成你想要的,你可以使用DateTime::createFromFormat():

$d = DateTime::createFromFormat('d-m-Y', '22-09-2008');
if ($d === false) {
    die("Woah, that date doesn't look right!");
}
echo $d->format('Y-m-d'), PHP_EOL;
// prints 2008-09-22

在这种情况下很明显,但是例如03-04-2008可能是4月3日或3月4日,这取决于你来自哪里:)

使用PHP函数strtotime()

echo strtotime('2019/06/06');

date -格式化本地时间/日期

使用strtotime()函数,您可以轻松地将日期转换为时间戳

<?php
// set default timezone
date_default_timezone_set('America/Los_Angeles');

//define date and time
$date = date("d M Y H:i:s");

// output
echo strtotime($date);
?> 

更多信息:http://php.net/manual/en/function.strtotime.php

在线转换工具:http://freeonlinetools24.com/

如果已经有了该格式的日期,则只需要在PHP中调用“strtotime”函数。

$date = '22-09-2008';
$timestamp = strtotime($date);
echo $timestamp; // 1222041600

或在单行中:

echo strtotime('22-09-2008');

简短而简单。