I want to convert DOMTimeStamp from Geolocation in PHP in order to save it in DATETIME in the database. How can I accomplish this?
Asked
Active
Viewed 458 times
0
-
Show us a sample value and what you've tried so far – John Conde Apr 23 '14 at 13:07
-
I don't have any example because I "must" save it in DOMTimeStamp in the database. I don't know how to convert it to DATETIME in PHP – Airikr Apr 23 '14 at 13:09
-
did you reviewed this answer: http://stackoverflow.com/questions/3089308/converting-domtimestamp-to-localized-hhmmss-mm-dd-yy-via-javascript – Jason OOO Apr 23 '14 at 13:09
-
You can't echo it out to your screen? – John Conde Apr 23 '14 at 13:09
-
@JasonOOO Yes. I'm using that function in JavaScript but I want to save the DOMTimeStamp in DATETIMe instead. To do that, I must convert it to DATETIME in PHP, like `date('Y-m-d H:i:s')` – Airikr Apr 23 '14 at 13:11
-
You could always hand the timestamp directly to the database... e.g. using MySQL's [`FROM_UNIXTIME()`](http://dev.mysql.com/doc/en/date-and-time-functions.html#function_from-unixtime) function. – eggyal Apr 23 '14 at 13:12
-
Have you read http://php.net/manual/en/datetime.construct.php and http://php.net/manual/en/datetime.format.php ? The PHP manual is pretty well written and has good examples. – Daniel Convissor Apr 23 '14 at 13:13
1 Answers
6
It's a just a timestamp in milliseconds. Just convert it to seconds and then use PHP's built in Date/Time functionality to convert it:
$timestamp = floor($domtimestamp / 1000); // Get seconds from milliseconds
$datetime = new DateTime('@'.$timestamp);
echo $datetime->format('Y-m-d H:i:s');

John Conde
- 217,595
- 99
- 455
- 496
-
Your answer solved my issue :) Many thanks! I'll accept it as soon as I can – Airikr Apr 23 '14 at 13:15