if you have an ambiguous date like "05/04/2014" (which is dd/mm/yyyy but just by looking at it could be month first too), how can you convert it to a PHP unix timestamp?

It looks like https://php.net/manual/en/function.strtotime.php is close to what I want, but there is no way to specify the date format as being day first?

有帮助吗?

解决方案

xdazz's answer is almost correct, but you will absorb the current time if you do that without removing it explicitly.

<?
$dt = DateTime::createFromFormat("m/d/Y", "05/04/2014");
$dt->setTime(0,0,0); # remove current time.
echo $dt->getTimestamp();
?>

In understand why we need line 2, replace line 3 with

  var_dump($dt);

Then, delete line 2, and observe that there is a time in addition to the desired date.

其他提示

you can use PHP fucntion date_format:

$date = date_create('2000-01-01');
echo date_format($date, 'm-d-Y H:i:s');

http://www.php.net/manual/en/datetime.format.php

See DateTime::createFromFormat and DateTime::getTimestamp:

$date = DateTime::createFromFormat('d/m/Y', '05/04/2014');
echo $date->getTimestamp();
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top