Peter Bushnell
Peter Bushnell

Reputation: 938

PHP: Convert epoch to MySQL DateTime format

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.

Upvotes: 4

Views: 4381

Answers (3)

bIgBoY
bIgBoY

Reputation: 417

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

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

Upvotes: 0

Vineet1982
Vineet1982

Reputation: 7918

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

Upvotes: 0

Hanky Panky
Hanky Panky

Reputation: 46900

PHP

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

MySQL

FROM_UNIXTIME(timestampColumn)

Nothing more to it

Upvotes: 15

Related Questions