Re: date & time conversion confusion
| From: | Thomas Deliduka | Date: | Mon, 06 Nov 2000 20:40:25 +0000 |
| Subject: | Re: date & time conversion confusion | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-23977@lists.php.net to get a copy of this message | ||
On 11/6/00 3:15 PM this was written:
> i'm having trouble figuring out how to convert an existing date & time into
> a more friendly format in PHP. I've used MySQL's date format which stores
> the values as YYYY-MM-DD. Now when I select these values from the database
> and display them on a web page I'd like to format them along the lines of
> MM-DD or Jan 01 or January 1 etc. I've looked into PHP's built-in functions
> time() mktime() getdate() etc, but none seem to work with existing values.
> Can anyone shed some light as to how I would go about doing this? I know in
> Perl there are tons of external libraries to do such things... are they
> available for PHP? Another issue is how I go about converting 24 hour time
> (00:00:00) into more friendly "XXam". Any help would be greatly
> appreciated.
I could be going about this backwards compared to others but I setup an
array of the months:
$months = array("January","February"......)
Then I get the value from the db and explode it into an array itself:
$thedate = explode("-",<value from db>);
Then to echo it I do:
echo $months[$thedate[1]-1] . " " . $thedate[2] . ", " . $thedate[0];
In the case of 24 hour time to 12 hour. I just parse out the hour:
$hour = substr($time,0,2);
if ($hour < 12) {
$hour = $hour; //for teaching purposes
$ap = "am";
} else {
if ($hour != 12) {
$hour = $hour - 12;
}
$ap = "pm";
}
echo $hour . $ap;
I guess I take the long way with everything though, I'm sure there's easier
ways.
--
Thomas Deliduka
IT Manager
-------------------------
New Eve Media
The Solution To Your Internet Angst
http://www.neweve.com/