4

I have to store epoch timestamps into a MySQL database DateTime column. I need to be able to convert the epoch into the DateTime form to be able to add it to the database. The epoch data is grabbed from an external source so we have no control over that, the database is also established and should be left as it is. We just need to be able to convert between the two in PHP.

Peter Bushnell
  • 918
  • 1
  • 13
  • 30
  • There're many [epochs](https://en.wikipedia.org/wiki/Epoch_%28reference_date%29)... I presume you mean [Unix epoch](https://en.wikipedia.org/wiki/Unix_time) :-? – Álvaro González Dec 10 '15 at 10:52

3 Answers3

14

PHP

date("Y-m-d H:i:s",$epochTS);

MySQL

FROM_UNIXTIME(timestampColumn)

Nothing more to it

Hanky Panky
  • 46,730
  • 8
  • 72
  • 95
0

No need to do any convert for MySQL or PHP just or enter the same to database

echo date('r', $epoch);

strotime() is the function used to convert datetime to epoch. Documentation

Vineet1982
  • 7,730
  • 4
  • 32
  • 67
0

strotime() is the function used to convert datetime to epoch. Documentation

time() is the function used to convert epoch to datetime. Documentation

bIgBoY
  • 417
  • 2
  • 12
  • also read [this](http://stackoverflow.com/questions/2850624/converting-timestamp-to-unix-time-in-php). – bIgBoY Dec 10 '15 at 10:27