datetime到时间戳


datetime to timestamp

可能的重复:
反日期函数?-不是strtotime
从这样的日期2011-02-27 02:04:46可以得到UNIX时间吗?

你好,

我们有这个函数可以将时间戳转换为日期时间:

$datetime = date('Y-m-d H:i:s', $timestamp);

有没有相反的功能?

datetime到时间戳。

感谢

$timestamp = strtotime($datetime);

或者,如果您对格式有信心,可以用explode()甚至substr分割字符串,并将必要的部分传递给mktime函数。

请注意,如果使用稍微非常规的格式,strtotime有时会出错。

编辑:

一个真正准确的方法是,如果你知道你的输入格式,那就是使用DateTime::createFromFormat,例如:

$dateTimeObject = 'DateTime::createFromFormat('G:i', '9:30');
$dateTimeObject->format('H:i');

请参阅http://php.net/manual/en/function.date.php用于格式化指南,以及http://php.net/manual/en/datetime.createfromformat.php以获取关于上述方法的信息。

$timestamp = strtotime('12-04-2010');