note 20101 added to ref.datetime
| From: | phpman at no dot spam dot priorwebsites dot com | Date: | Fri, 22 Mar 2002 09:39:03 +0000 |
| Subject: | note 20101 added to ref.datetime | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-28070@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;
}
--
http://www.php.net/manual/en/ref.datetime.php
http://master.php.net/manage/user-notes.php?action=edit+20101
http://master.php.net/manage/user-notes.php?action=delete+20101
http://master.php.net/manage/user-notes.php?action=reject+20101