note 112253 added to function.date

From: Date: Thu, 23 May 2013 12:28:58 +0000
Subject: note 112253 added to function.date
Groups: php.notes 
Request: Send a blank email to php-notes+get-194887@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. <? 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"; /* ... */ ?> ---- Server IP: 69.147.83.199 Probable Submitter: 132.170.92.239 ---- Manual Page -- http://php.net/manual/en/function.date.php Edit -- https://master.php.net/note/edit/112253 Del: integrated -- https://master.php.net/note/delete/112253/integrated Del: useless -- https://master.php.net/note/delete/112253/useless Del: bad code -- https://master.php.net/note/delete/112253/bad+code Del: spam -- https://master.php.net/note/delete/112253/spam Del: non-english -- https://master.php.net/note/delete/112253/non-english Del: in docs -- https://master.php.net/note/delete/112253/in+docs Del: other reasons-- https://master.php.net/note/delete/112253 Reject -- https://master.php.net/note/reject/112253 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#194887) next »