How to display a different 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
User avatar
fluvly
Forum Newbie
Posts: 10
Joined: Wed Sep 23, 2009 10:08 am

How to display a different date format

Post by fluvly »

I have this date and time inside a variable, taken from a database:

Code: Select all

$datetime = "2009-09-17 12:20:08";
I would like to extract just the date (without the time) and show it as "17/09/2009" instead. Is there a way to do this without the "explode" function?

Thanks to anyone willing to help!
iamngk
Forum Commoner
Posts: 50
Joined: Mon Jun 29, 2009 2:20 am

Re: How to display a different date format

Post by iamngk »

You can use strtotime and date function.

Code: Select all

$datetime = "2009-09-17 12:20:08";
$newdate = date("d/m/Y",strtotime($datetime));
User avatar
fluvly
Forum Newbie
Posts: 10
Joined: Wed Sep 23, 2009 10:08 am

Re: How to display a different date format

Post by fluvly »

Thanks a lot!
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: How to display a different date format

Post by pickle »

Can you affect the query? This can all be done in SQL.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
User avatar
fluvly
Forum Newbie
Posts: 10
Joined: Wed Sep 23, 2009 10:08 am

Re: How to display a different date format

Post by fluvly »

Yes, I can. What would be the best way to do that?
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: How to display a different date format

Post by pickle »

Well, it looks like the value you're getting is from a datetime column. The DATE_FORMAT() function would work well here.

http://dev.mysql.com/doc/refman/5.0/en/ ... ate-format
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
User avatar
fluvly
Forum Newbie
Posts: 10
Joined: Wed Sep 23, 2009 10:08 am

Re: How to display a different date format

Post by fluvly »

Thank you very much!
Post Reply