0

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?

0

3 Answers 3

2

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.

Sign up to request clarification or add additional context in comments.

4 Comments

Thank you. Could you please elaborate on what you mean by "absorb the current time"? I don't understand why you are doing setTime() afterwards. Thanks
@b85411, It means that your timestamp will be 05/04/2014 + the number of seconds since the beginning of today on the clock in your current system.
By current system - do you mean the server running the script? Surely PHP isn't able to determine local system time? Thanks
@b85411, My tests seem to show that it is UTC, but I am not 100% sure about that. But yes, it would be getting the actual time from the server running the script.
0

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

Comments

0

See DateTime::createFromFormat and DateTime::getTimestamp:

$date = DateTime::createFromFormat('d/m/Y', '05/04/2014');
echo $date->getTimestamp();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.