PHP-MYSQL: Converting Unix Timestamp to DateTime and vice versa PHP-MYSQL: Converting Unix Timestamp to DateTime and vice versa mysql mysql

PHP-MYSQL: Converting Unix Timestamp to DateTime and vice versa


Your date format is wrong... i is for minute, not m (months).

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

A few side notes:

  • There's no need to re-assign, i.e. $unixTimestamp = $unixTimestamp;
  • Since you're using PHP > 5.3. you may be interested in the new DateTime object.


The problem lies in the function you wrote:

return date("Y-m-d H:m:s", $unixTimestamp);

You specify month(m) in both the date and time portions. You should replace the second m with i, the correct flag for hours when using date() in PHP.

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

Depending on your needs you might find the PHP function strtotime() useful enough.


Why not simply use the FROM_UNIXTIME function in mysql ?