Page 1 of 1

How to display a different date format

Posted: Fri Sep 25, 2009 3:20 am
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!

Re: How to display a different date format

Posted: Fri Sep 25, 2009 3:48 am
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));

Re: How to display a different date format

Posted: Fri Sep 25, 2009 4:48 am
by fluvly
Thanks a lot!

Re: How to display a different date format

Posted: Fri Sep 25, 2009 10:12 am
by pickle
Can you affect the query? This can all be done in SQL.

Re: How to display a different date format

Posted: Fri Sep 25, 2009 10:23 am
by fluvly
Yes, I can. What would be the best way to do that?

Re: How to display a different date format

Posted: Fri Sep 25, 2009 10:40 am
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

Re: How to display a different date format

Posted: Fri Sep 25, 2009 10:54 am
by fluvly
Thank you very much!