Is there anyone sitting on a PHP function to convert a date of format 0000-00-00 00:00:00(datetimesql) to unix timestamp?
Thanks in advance.
Is there anyone sitting on a PHP function to convert a date of format 0000-00-00 00:00:00(datetimesql) to unix timestamp?
Thanks in advance.
Use strptime()
to parse the time and turn it into a structured array.
Then pass the results of that into the mktime()
function to get a UNIX timestamp.
Another option as you have tagged this question with SQL: the MySQL functions FROM_UNIXTIME
and UNIX_TIMESTAMP
-> MySQL manual
SELECT UNIX_TIMESTAMP(datetime_column) FROM table
This usually is faster than a PHP function call.
@bartek - As you noted, PHP's strtotime function is perfect for this. It can handle most common date formats, including strings like "tomorrow" or "+5 months".
Encapusulating wvanbergen's solution into a function (for your convenience)
//Convert SQL datetime to unixtime -- by johnboiles
function datetimeToUnixtime($datetime){
$result = mysql_query("select unix_timestamp('$datetime')");
$unixtime = mysql_fetch_array($result);
return $unixtime[0];
}