|
Date conversion - PHP
A simple piece of code to show you some examples of date reformatting in PHP. Frequently, you'll get dates in a format such as the ones I've started with here and want to re-arrange them, or do something more sophisticated.
My first re-arrangement simply explodes the date string and reforms it. My second converts it into seconds from 1st January 1970 (and I've taken the hour of 12, Midday, to complete the parameters to mktime) then reformats it with the date function.
<?php
$dates = array("2006-10-12","2004-05-08","2007-07-21");
foreach ($dates as $timestamp) {
$parts = explode("-",$timestamp);
print "<p>Date is $parts[2]/$parts[1]/$parts[0]</p>";
$thatis = mktime(12,0,0,$parts[1],$parts[2],$parts[0]);
$nicedate = date("l, jS F Y",$thatis);
print "<p>Date is $nicedate</p>";
}
?>
The results:
Date is 12/10/2006
Date is Thursday, 12th October 2006
Date is 08/05/2004
Date is Saturday, 8th May 2004
Date is 21/07/2007
Date is Saturday, 21st July 2007
All done with easy function calls ... as we say in PHP "There's a function to do that" (written 2006-12-26 17:35:04)
| Commentator | says ... | | Alex Baskov: | i guess you don't use this method now...
if you do, then you may try the following:
date("l, jS F Y", strtotime("2008-01-30 10:12:47")); (comment added 2008-01-30 14:54:10) |
Associated topics are indexed under H107 - String Handling in PHP
Some other Articles
Moving files between Windows / DOS and Linux / UnixWell House Manor and Beechfield House, Hotels, MelkshamDates, times, clickable diarys in PHPPassing GET parameters through Apache mod_rewriteDate conversion - PHPFriends and familyApache httpd and Apache Tomcat together tipsStirling at nightOld dog, old tricksRoom at the Inn, Guy at the station
|
2259 posts, page by page
Link to page ... 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46 at 50 posts per page
This is a page archived from The Horse's Mouth at
http://www.wellho.net/horse/ -
the diary and writings of Graham Ellis.
Every attempt was made to provide current information at the time the
page was written, but things do move forward in our business - new software
releases, price changes, new techniques. Please check back via
our main site for current courses,
prices, versions, etc - any mention of a price in "The Horse's Mouth"
cannot be taken as an offer to supply at that price.
Link to Ezine home page (for reading).
Link to Blogging home page (to add comments).
|
|