Date conversion - PHP
Archive - Originally posted on "The Horse's Mouth" - 2006-12-26 17:35:04 - Graham EllisA 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"