PHPRO.ORG

PHP date functions such as strtotime() and time() and date() all return a value as a UNIX TIMESTAMP. This is great for date manipulation but becomes a problem when we need a date to INSERT into a MySQL database. The format of a MySQL TIMESTAMP is yyyy-mm-dd hh:mm:ss and to meet this need a simple PHP can be utilized to convert any UNIX TIMESTAMP into a MySQL TIMESTAMP.


<?php

/**
 *
 * @convert UNIX TIMESTAMP to MySQL TIMESTAMP
 *
 * @param int $timestamp
 *
 * @return string
 *
 */
function unixToMySQL($timestamp)
{
    return 
date('Y-m-d H:i:s'$timestamp);
}


/*** example usage ***/
$time time();
echo 
unixToMySQL($time);

?>