note 47749 deleted from function.mktime by derick
| From: | derick@php.net | Date: | Sun, 24 Sep 2006 12:03:10 +0000 |
| Subject: | note 47749 deleted from function.mktime by derick | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-117522@lists.php.net to get a copy of this message | ||
Note Submitter: cp at u-help dot org
----
I once had to deal with dates coming in as a sting from a mysql database (surprise, surprise ;-))
without having any option to select anything differen (i.e. using mysql's UNIX_TIMESTAMP)
This caused me a lot of problems and I couldn't seem to find anything anywhere that would just
take the entire lot as an input to convert it to unix-timestamp.
Maybe someone else finds the below function useful to convert these date/time strings into
unix-timestamps
<?php
function dttm2unixtime($dttm2timestamp_in){
// returns unixtime stamp for a given date time string that comes from DB
$date_time = explode(" ", $dttm2timestamp_in);
$date = explode("-",$date_time[0]);
$time = explode(":",$date_time[1]);
unset($date_time);
list($year, $month, $day)=$date;
list($hour,$minute,$second)=$time;
return mktime(intval($hour), intval($minute), intval($second), intval($month), intval($day),
intval($year));
}
?>
A mysql date-time value like "2004-11-28 01:34:20" would be converted to this: 1101605660
As a verification, date("Y-m-d H:i:s", 1101605660) would return the same as the value
passed in, i.e. 2004-11-28 01:34:20
Using the function above, you don't have to include any fancy parsing into the retrieval of the
results...
hope it helps ;)