note 26655 modified in function.date by maxim
| From: | maxim@php.net | Date: | Fri, 08 Nov 2002 11:28:08 +0000 |
| Subject: | note 26655 modified in function.date by maxim | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-39146@lists.php.net to get a copy of this message | ||
[Editor's note:
alternatively, strtotime() function might be used.
-Maxim]
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.
--was--
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