如何将收到的日期时间对象转换为 php 中的时间戳?
how to convert received datetime object to timestamp in php?
我从 POST 收到日期时间对象(例如 30/06/2015 02:04 PM
)。稍后我需要在 php 代码中将其用作时间戳,因此变量 $begin_timestamp = $_POST['date'];
将包含时间戳而不是此日期时间。我怎样才能在那个时候快速改变它?
你可以使用静态方法 <a href="http://php.net/manual/en/datetime.createfromformat.php" rel="nofollow">createFromFormat()</a>
of the <a href="http://php.net/manual/en/class.datetime.php" rel="nofollow">DateTime</a>
class:
<?php
$date = DateTime::createFromFormat('d/m/Y H:i A', '30/06/2015 02:04 PM');
$timestamp = $date->getTimestamp(); // 1435665840
?>
我从 POST 收到日期时间对象(例如 30/06/2015 02:04 PM
)。稍后我需要在 php 代码中将其用作时间戳,因此变量 $begin_timestamp = $_POST['date'];
将包含时间戳而不是此日期时间。我怎样才能在那个时候快速改变它?
你可以使用静态方法 <a href="http://php.net/manual/en/datetime.createfromformat.php" rel="nofollow">createFromFormat()</a>
of the <a href="http://php.net/manual/en/class.datetime.php" rel="nofollow">DateTime</a>
class:
<?php
$date = DateTime::createFromFormat('d/m/Y H:i A', '30/06/2015 02:04 PM');
$timestamp = $date->getTimestamp(); // 1435665840
?>