Javascript getTime to php date
Asked Answered
A

2

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
Amphitryon answered 10/8, 2012 at 7:36 Comment(0)
T
9

You use PHP's date() function:

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

Updated, thanks Yoshi.

Transcaucasia answered 10/8, 2012 at 7:41 Comment(5)
that's what i did but its off by a lotAmphitryon
@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.Transcaucasia
@chris That's expectable as js' getTime includes milliseconds, while date does not expect them. Try dividing the timestamp by 1000 first.Externality
echo date("m/d/Y", '134630999900');Amphitryon
+1 ;) though the obvious error checking on $_POST["time_field"] should not be forgotten in production code.Externality
H
0

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

Holocene answered 10/8, 2012 at 7:45 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.