質問

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