note 112253 modified in function.date by danbrown
| From: | danbrown@php.net | Date: | Wed, 05 Jun 2013 17:54:53 +0000 |
| Subject: | note 112253 modified in function.date by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-195079@lists.php.net to get a copy of this message | ||
If anyone needs a really fast function for converting a datetime string (i.e. as retrieved from a
MySQL DATETIME entry) into a human-friendly time output analogous to date($format, $time),
here's a useful function.
<?php
function fdate($datetimestring = '1970-01-01 00:00:00', $format = 'U') {
// Create a datetime object, return it formatted
// If you want to give credit for this somewhere, thanks.
// You really don't have to though; this is kinda obvious
$dt = new DateTime($datetimestring);
return $dt->format($format);
}
?>
The main purpose of this is to reduce lines of code and allow inline coding. For example:
<?php
/* ... */
echo "This page was submitted on ".fdate($row['created'], 'F j, Y g:i:s
A')." and last modified ".fdate($row['modified'], 'F j, Y g:i:s
A')."<br />\n";
/* ... */
?>
--was--
If anyone needs a really fast function for converting a datetime string (i.e. as retrieved from a
MySQL DATETIME entry) into a human-friendly time output analogous to date($format, $time),
here's a useful function.
<?
function fdate($datetimestring = '1970-01-01 00:00:00', $format = 'U') {
// Create a datetime object, return it formatted
// If you want to give credit for this somewhere, thanks.
// You really don't have to though; this is kinda obvious
$dt = new DateTime($datetimestring);
return $dt->format($format);
}
?>
The main purpose of this is to reduce lines of code and allow inline coding. For example:
<?
/* ... */
echo "This page was submitted on ".fdate($row['created'], 'F j, Y g:i:s
A')." and last modified ".fdate($row['modified'], 'F j, Y g:i:s
A')."<br />\n";
/* ... */
?>
http://php.net/manual/en/function.date.php