1

I have a user input where the user inputs time and date as '9.30am' and '01/02/2012'. I am trying to convert this to a unix timestamp to aid ordering when dragging the data back out of the database but strptime() is confusing me as I am unsure as to whether this is actually returning a unix timestamp that I need.

4

4 回答 4

1

您可以使用:strtotime

PHP.net 示例:

echo strtotime("now"), "\n";
echo strtotime("10 September 2000"), "\n";
echo strtotime("+1 day"), "\n";
echo strtotime("+1 week"), "\n";
echo strtotime("+1 week 2 days 4 hours 2 seconds"), "\n";
echo strtotime("next Thursday"), "\n";
echo strtotime("last Monday"), "\n";
于 2013-01-06T10:29:09.080 回答
0
$date = DateTime::createFromFormat('H:ia d/m/Y' '9:30am 1/2/2012');
$timestamp = $date->getTimestamp();

是你正在寻找的。http://www.php.net/datetime

于 2013-01-06T10:39:03.887 回答
0

你可以使用 strtotime()

通过放入变量来调用它。

strtotime("9.30 1/2/2012");

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

于 2013-01-06T10:28:21.433 回答
0

使用 PHP 的内置 DateTime 类...

$objDate = DateTime::createFromFormat('H:ia d/m/Y', '9:30am 1/2/2012');
$timestamp = $objDate->getTimestamp();

...返回一个 Unix 时间戳。

文档:

  1. PHP 日期时间类

    一个。日期时间::createFromFormat

    湾。日期时间::getTimestamp

要添加变量,请像这样使用它...

$objDate = DateTime::createFromFormat('H:ia d/m/Y', "$time $date");
$timestamp = $objDate->getTimestamp();

或者...

$time_date = sprintf("%s %s", $time, $date); // concatenates into a single var
$objDate = DateTime::createFromFormat('H:ia d/m/Y', $time_date);
$timestamp = $objDate->getTimestamp();

注意:确保您的变量符合以下...

$time= hh:mm(并附加“am”或“pm”)

$date= dd/mm/yyyy

于 2013-01-06T11:18:10.370 回答