4

I'm trying to do something incredibly simple - convert a timestamp to a string in php with the date() function.

Code is as follows:

$test = date('d/m/y','1407974400000');
echo $test;

I expect the answer to be 14/8/14.

If I check on http://www.epochconverter.com/ that also gives this answer.

Yet the output produced from the above PHP is

07/12/86

I'm pretty sure I'm doing something completely stupid here - any able to help?

Thanks,

Chris

  • `time()` for me currently outputs `1408013482`. You've got 3 digits too many. You should use seconds. – Jonathon Aug 14 '14 at 10:52
  • Did you get that value from javascript? Javascript timestamps are different to PHP for some reason I have yet to totally understand. They tend to be in micro seconds and if its from a smart phone it gets even weirder as android and apple js date timestamps are also bigger but also different to each other. There be Dragons. – RiggsFolly Aug 14 '14 at 10:55
  • Yes I did, the wider script is part of an AJAX call initiated by JS. I didn't know there were those differences so will look out of them. Thanks. – Chris Hykin Aug 14 '14 at 10:58

2 Answers2

4

You had 3 zeros more, use seconds instead of milliseconds:

$test = date('d/m/y','1407974400');
echo $test;
Ende Neu
  • 15,581
  • 5
  • 57
  • 68
1

You should use "seconds" not "milliseconds" on date function as timestamp.

$test = date('d/m/y','1407974400');
echo $test;
Volkan Ulukut
  • 4,230
  • 1
  • 20
  • 38