Change date format

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
S_henry
Forum Contributor
Posts: 148
Joined: Sun Jan 25, 2004 10:25 pm
Location: M'sia

Change date format

Post by S_henry »

Currently in my table (in database), i store date record in 'dd.mm.yyyy' format. The problem is i don't know how to display that date in 'MMM dd, YYYY' format. i've tried all the way but still can't find the right way. Anybody can help me pls?
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post by Burrito »

date()

ex:

Code: Select all

echo date("M d, Y",time());
S_henry
Forum Contributor
Posts: 148
Joined: Sun Jan 25, 2004 10:25 pm
Location: M'sia

Post by S_henry »

If i not mistake, that code will display the current date, right? But my prob is to convert the date record from database (in 'dd.mm.yyyy' format) to 'MMM dd, YYYY' format. For example, record in database is '20.05.2005'. I want to display that record as 'May 20, 2005'. Anybody can help?
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

1-) Use a correct datatype for your column => date

2-) Lookup the date & time functions in your rdbms manual.
Mysql has for example DATE_FORMAT.
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

timvw wrote:1-) Use a correct datatype for your column => date

2-) Lookup the date & time functions in your rdbms manual.
Mysql has for example DATE_FORMAT.
or failing that (although i strongly advise you do as timvw says)....

[Note... we get a lot of questions of this nature]

Code: Select all

$date = '02.06.2005';
$bits = explode('.', $date);
$date_stamp = strtotime($bits[1].'/'.$bits[0].'/'.$bits[2]); //Use a U.S. format for strtotime() [mm/dd/YYYY]
echo date("d M, Y", $date_stamp);
S_henry
Forum Contributor
Posts: 148
Joined: Sun Jan 25, 2004 10:25 pm
Location: M'sia

Post by S_henry »

OK. Now the prob is solved already. Currently i'm using d11wtq's way. But just want to know, in php, can we convert the string to date? Just like we convert int to string..
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

S_henry
Forum Contributor
Posts: 148
Joined: Sun Jan 25, 2004 10:25 pm
Location: M'sia

Post by S_henry »

OK. Thanx guys.. :D
Post Reply