tags:

views:

144

answers:

8

I am pulling the dates of various posts from a database. The dates are in the following format:

2009-08-12

Numeric Year - Numeric Month - Numeric Day

How can I reformat these dates to something more user friendly like:

August 12, 2009

Numeric Month Numeric Date, Numeric Year

Assuming that the date gotten from the mysql database is stored in a variable called:

$date = $row['date_selected'];
+4  A: 
$new_format = date("Your Date String", strtotime($date));

See:
- http://php.net/strtotime
- http://php.net/date

Basically, if strtotime() can read it correctly, you can reformat it anyway you please.

In this case, Year - Month - Day is a properly recognized strtotime() format, this might not be the case for other formats.

Chacha102
You can't enforce correct handling by just calling strtotime. For example, somebody could switch the month and day around and strtotime would not handle it.
Billy ONeal
Like I said, if `strtotime()` can read it correctly, you can do it.
Chacha102
but there no to decise programmatically if a date is correctly formatted if you only have the string "2009-08-02". For that to be possible you would have to have seperate input fields for year, month and day.
Femaref
@Femaref: Yes, but if you use DateTime::createFromFormat, at least it does range checking to ensure that if you meant for a month to be in that spot, it won't accept a value like 31
Billy ONeal
@Femaref: For example, is 2009-08-12 August 12, 2009, or is it December 8, 2009? There's no way to specify which one you want to strtotime.
Billy ONeal
@Billy But he isn't talking about a weird format. For this specific problem, there is no need for DateTime, as Year - Month - Day is a correct strtotime format.
Chacha102
@Chacha102: Sure, that'll work great until somebody tries to run the script on a server with different locale settings.
Billy ONeal
The strtotime method is the 'standard', and if your server's locale settings expect month-day-year, it will work just fine, but @Billy ONeal's method is safer, assuming you have php 5.3 available, which on a shared host is definitely not automatic yet.
sprugman
@Billy There are a bunch more problems you'll have to solve when that happens...
Chacha102
+2  A: 

date("F d, Y", strtotime($input))

Femaref
You can't enforce correct handling by just calling strtotime. For example, somebody could switch the month and day around and strtotime would not handle it.
Billy ONeal
since this was selected as the answer, despite having fewer votes than @Billy's, I'll repeat my comment from @chacha's answer below:The strtotime method is the 'standard', and if your server's locale settings expect month-day-year, it will work just fine, but @Billy's method is safer, assuming you have php 5.3 available, which on a shared host is definitely not a safe assumption yet.
sprugman
+6  A: 

Unlike the strtotime based examples, this allows you to ensure the month and day are interpreted in the correct order regardless of locale settings specified on the server.

$date = DateTime::createFromFormat('Y-m-d', '2009-08-12');
$output = $date->format('F j, Y');
Billy ONeal
Why the downvote?
Billy ONeal
+1: `DateTime` should always be preferred instead of `strtotime`.
dalle
A: 
<?php
echo date('F j, Y', strtotime($date));
icio
A: 

You might want to look at the php function strtotime:

http://php.net/manual/en/function.strtotime.php

It'll parse a large number of date representations to a Unix timestamp.

Then use the date function.

Weston C
A: 

Use it like this:
// February 1, 2005
print date ("F j, Y", mktime (0,0,0,14,1,2004));

Oyeme
A: 

Using strtodate or explode to split the date into its different components, you can then use the date function with the appropriate format string:http://php.net/manual/en/function.date.php

$date = "2009-08-12";
list($year,$month,$day) = explode("-",$date);
$formattedDate = date("F d, Y", mktime(0,0,0,$month,$day,$year));

Outputs: "August 12, 2009"

jkilbride
+3  A: 

You might consider doing your date formatting in MySQL with your select statement:

DATE_FORMAT(date,'%M %e, %Y') as date_selected

http://www.w3schools.com/sql/func_date_format.asp

gurun8