Forum Moderators: coopster
For example:
"20031229121500" is the mysql record pulled from a DATETIME field.
So I need to do this with PHP:
(20031229121500 + 4 hours) = 20031229121900
(20031229121500 + 1 day) = 20031230121500
(20031229121500 + 30 seconds) = 20031229121530
Can anyone SHOW me exactly how this is done, with a code example? I *think* is has something to do with converting the mysql datetime record to a UNIX timestamp, adding or subtracting from this, then converting this BACK to the mysql datetime format. But how?
Thanks for the help!
-Arie
[edited by: ariev at 3:56 am (utc) on Dec. 1, 2003]
For instance:
SELECT DATE_ADD(NOW(), INTERVAL 1 DAY);
Hope it helps.
-Andy
function returnSQLTimestamp($timestamp)
{
// Function returnSQLTimestamp
// by elnino (25-03-2002)
// version 0.1
// Converts an Unix-timestamp to a MySQL-timestamp.
return strftime('%Y%m%d%H%M%S', $timestamp);
}
function returnUnixTimestamp($timestamp)
{
// Function returnUnixTimestamp
// by elnino (25-03-2002)
// version 0.1
// Converts an MySQL-timestamp to a Unix-timestamp.
$year = substr($timestamp, 0, 4);
$month = substr($timestamp, 4, 2);
$day = substr($timestamp, 6, 2);
$hour = substr($timestamp, 8, 2);
$min = substr($timestamp, 10, 2);
$sec = substr($timestamp, 12, 2);
return mktime($hour, $min, $sec, $month, $day, $year);
}
function AddSecToSqlDate($timestamp, $seconds)
{
// Function returnUnixTimestamp
// by elnino (25-03-2002)
// version 0.1
// Converts an MySQL-timestamp to a Unix-timestamp.
$year = substr($timestamp, 0, 4);
$month = substr($timestamp, 4, 2);
$day = substr($timestamp, 6, 2);
$hour = substr($timestamp, 8, 2);
$min = substr($timestamp, 10, 2);
$sec = substr($timestamp, 12, 2);
return returnSQLTimestamp(mktime($hour, $min, $sec+$seconds, $month, $day, $year));
}
function SubtSecFromSqlDate($timestamp, $seconds)
{
// Function returnUnixTimestamp
// by elnino (25-03-2002)
// version 0.1
// Converts an MySQL-timestamp to a Unix-timestamp.
$year = substr($timestamp, 0, 4);
$month = substr($timestamp, 4, 2);
$day = substr($timestamp, 6, 2);
$hour = substr($timestamp, 8, 2);
$min = substr($timestamp, 10, 2);
$sec = substr($timestamp, 12, 2);
return returnSQLTimestamp(mktime($hour, $min, $sec-$seconds, $month, $day, $year));
}