6

I have javascript that turns dates in my view to a time string using getTime(). That then is stored as a value for an option in my select form. Once it is passed to php, how do I turn that into a php date?

I have done:

echo date("m/d/Y", '1345618799000');

1345618799000 = Tue Aug 21 2012
cdub
  • 21,144
  • 49
  • 157
  • 274

2 Answers2

9

You use PHP's date() function:

date("Format here (see documentation)", round($_POST["time_field"]/1000));

Updated, thanks Yoshi.

Aubin
  • 13,790
  • 8
  • 55
  • 78
John V.
  • 4,474
  • 4
  • 23
  • 26
  • that's what i did but its off by a lot – cdub Aug 10 '12 at 07:43
  • @chris What did you use for format? Also, if you did try something first (which is something nearly everyone here requires for a question) add it to the question. – John V. Aug 10 '12 at 07:43
  • 1
    @chris That's expectable as js' `getTime` includes milliseconds, while `date` does not expect them. Try dividing the timestamp by 1000 first. – Yoshi Aug 10 '12 at 07:45
  • echo date("m/d/Y", '134630999900'); – cdub Aug 10 '12 at 07:45
  • @Yoshi AH, thanks, I didn't know that, I'll update my answer. – John V. Aug 10 '12 at 07:46
  • 1
    +1 ;) though the obvious error checking on `$_POST["time_field"]` should not be forgotten in production code. – Yoshi Aug 10 '12 at 07:49
0

You may use PHP's strtotime() to change any string into a Unix timestamp; strtotime()

Vishal
  • 1,236
  • 1
  • 9
  • 16