note 52987 deleted from function.mktime by philip
| From: | philip@php.net | Date: | Fri, 03 Jun 2005 17:32:43 +0000 |
| Subject: | note 52987 deleted from function.mktime by philip | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-89879@lists.php.net to get a copy of this message | ||
Note Submitter: chrisrobins at NOSPAM dot gmail dot com
----
I wanted to covert MySQL TIMESTAMP's into a readable format and came up with this.
<?php
// MySQL returns TIMESTAMP's like '2005-05-20 01:26:21'
// line 1-3 removes (:) (space) and (-)
// line 5-10 extracts the timestamp
// line 12 makes the readable date using the $format and $timestamp
// line 14 returns the new $timestamp as a readable date
function timestamp($format, $timestamp)
{
$search = array ('/:/','/ /','/-/',);
$replace = array ('','','',);
$timestamp = preg_replace($search, $replace, $timestamp);
$year = substr($timestamp, 0, 4);
$month = substr($timestamp, 4, 2);
$day = substr($timestamp, 6, 2);
$hour = substr($timestamp, 8, 2);
$min = substr($timestamp, 10, 2);
$sec = substr($timestamp, 12, 2);
$new_timestamp = date($format, mktime($hour, $min, $sec, $month, $day, $year));
return $new_timestamp;
}
?>
You could call it like
<?php $date = timestamp("F j, Y, g:i a", $row['date']); ?>
I hope it helps someone.