如何将unix时间戳转换为mysql日期格式

I'm writing code to subtract two dates. It is for a contract type thingy, where user gets to see the number of days left for his contract to complete. Something like start_date_time="today" and end_date_time=y where the value of y is retrieved from the database (DATETIME type). It is in the mysql datetime format (yyyy-mm-dd HH:mm:ss).

<?php
include_once '../include/connections.php';
$id =$_REQUEST['uid'];
$result= mysql_query("SELECT * FROM data WHERE uid  = '$id'");
$test = mysql_fetch_array($result);
echo $test[14];
echo "<br /><br />";
$today=time();
$enddate=strtotime('$test[14]');
$timediff = $enddate - $today;
$days=intval($timediff/86400);
$remaining=$timediff%86400;
$hours=intval($remaining/3600);
$remaining=$remaining%3600;
$mins=intval($remaining/60);
$secs=$remaining%60;
echo "<br>".$days.' days '.$hours.' hours '.$mins.' minutes and '.$secs.' seconds.';
?>

When I echo $test[14];, I get the date and time as stored in the database which is (2012-09-26 00:00:00)

When i echo $today; then i get it in this format 1348381896. Now, how do i convert this format to the one retrieved from the db so that i can subtract the 2 dates and get the number of days and time left?

You can use these 2 functions to convert dates to each other,

Use this for timestamp to MySQL datetime:

$timestamp = '1348381896';
$date = date('Y-m-d H:i:s', $timestamp);
echo $date;

Use this one for MySQL datetime to timestamp:

$date = '2012-09-26 00:00:00';
$timestamp = strtotime($date);
echo $timestamp;

Also if you are willing to Subtract your dates in MySQL side, you can use the DATEDIFF and or TIMEDIFF functions:

Also you can work with timestamps in MySQL too, using TIMESTAMPDIFF and UNIX_TIMESTAMP functions ...

You could use PHP's DateTime classes for this:-

$today = new DateTime();

$mysqlDate = "2012-12-15 13:40:20"; //for example
// To match your code this would be $mysqlDate = $test[14];
$mysqlFormat = 'Y-m-d H:i:s';

$endDate = DateTime::createFromFormat($mysqlFormat, $mysqlDate);
$diff = $today->diff($endDate);
echo "You have {$diff->d} days, {$diff->h} hours and {$diff->m} minutes left";

This will give the following output:-

You have 22 days, 6 hours and 5 minutes left

(This will change depending on when you run the code).

$diff is an instance of DateInterval.

date formats acceptable to DateTime::createFromFormat() can be found here http://www.php.net/manual/en/function.date.php