note 26655 added to function.date

From: Date: Fri, 08 Nov 2002 11:19:09 +0000
Subject: note 26655 added to function.date
Groups: php.notes 
Request: Send a blank email to php-notes+get-39144@lists.php.net to get a copy of this message
These functions will let you swap a MySQL timestamp for a unix one and vice versa. I know there are easier ways out there but i just thought it may be useful. I hope this helps someone out there. function unix_date ($mysql_timestamp) { // change a mysql timestamp to unix timestamp $year = substr($mysql_timestamp, 0, 4); $month = substr($mysql_timestamp, 4, 2); $day = substr($mysql_timestamp, 6, 2); $hours = substr($mysql_timestamp, 8, 2); $mins = substr($mysql_timestamp, 10, 2); $secs = substr($mysql_timestamp, 12, 2); $date_time = sprintf("%04d-%02d-%02d %02d:%02d:%02d" , $year, $month, $day, $hours, $mins, $secs) ; $unix_date = strtotime ($date_time) ; return $unix_date ; } function mysql_date ($unix_timestamp) { // change a unix timestamp to mysql timestamp $mysql_date = date("YmdHis", $unix_timestamp) ; return $mysql_date ; } The mysql_date function may seem like a bit of overkill but may save a bit of time, and the unix_date is easier done by using a SQL function like UNIX_TIMESTAMP, but hey whatever works, it may come in handy. -- http://www.php.net/manual/en/function.date.php http://master.php.net/manage/user-notes.php?action=edit+26655 http://master.php.net/manage/user-notes.php?action=delete+26655 http://master.php.net/manage/user-notes.php?action=reject+26655

« previous php.notes (#39144) next »