note 20101 deleted from ref.datetime by victor
| From: | victor@php.net | Date: | Mon, 09 Dec 2002 18:54:00 +0000 |
| Subject: | note 20101 deleted from ref.datetime by victor | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-40618@lists.php.net to get a copy of this message | ||
The editor is going to tell me it's easier to use UNIX_TIMESTAMP (I know you're watching
us ed! ;) ), but I disagree. I often want to just do a "SELECT * FROM...", and this
function makes that possible.
I also see a guy above has also written basically the same function. Great minds think alike, or
fools seldom differ? ;)
/**
* Convert mysql timestamp to PHP timestamp.
*
* The format of a mysql date is "yyyymmddhhmmss" whereas
* PHP uses the (UNIX) format "sssssssssss" (number of seconds
* since 1970). Though it is possible to convert using
* the UNIX_TIMESTAMP function of mysql, it is sometimes more convenient
* to read in the timestamp variable just like any other (for example,
* by doing a "SELECT * FROM mytable"), and convert the timestamp
* field when it is in the program.
*
* This function simply does that conversion.
*
* Example:
* $sql_row = mysql_fetch_array( $result );
* $php_timestamp = mysql_to_php_timestamp($sql_row[my_timestamp_field]);
* $display_date = strftime("%d %B %Y %H:%M:%S", $php_timestamp);
*/
function mysql_to_php_timestamp( $mysql_date )
{
// mysql date looks like "yyyymmddhhmmss"
$year = substr( $mysql_date, 0, 4 );
$month = substr( $mysql_date, 4, 2 );
$day = substr( $mysql_date, 6, 2 );
$hour = substr( $mysql_date, 8, 2 );
$min = substr( $mysql_date, 10, 2 );
$sec = substr( $mysql_date, 12, 2 );
// Warning: mktime uses a strange order of arguments
$php_timestamp = mktime( $hour, $min, $sec, $month, $day, $year );
return $php_timestamp;
}