For Programmers: Free Programming Magazines  


Home > Archive > PHP Programming > October 2007 > Formatting a date with php from a mysql table









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Formatting a date with php from a mysql table
mantrid

2007-10-27, 7:01 pm

Hello
I have date and time in my mysql table in the form
2007-05-03 00:00:00
which I have dispayed on my webpage using
echo $selldatetime
I want to know how I can display it in the form
03-05-2007

Thanks


mantrid

2007-10-27, 7:01 pm

ok
I found it

echo date('d-m-Y', strtotime($selldatetime))

seems to work


"mantrid" <ian.dandav@virgin.net> wrote in message
news:4LMUi.269$ib1.161@newsfe3-win.ntli.net...
> Hello
> I have date and time in my mysql table in the form
> 2007-05-03 00:00:00
> which I have dispayed on my webpage using
> echo $selldatetime
> I want to know how I can display it in the form
> 03-05-2007
>
> Thanks
>
>



Michael Fesser

2007-10-27, 7:01 pm

..oO(mantrid)

>ok
>I found it
>
>echo date('d-m-Y', strtotime($selldatetime))
>
>seems to work


It does, but often it makes more sense to let MySQL return an already
formatted date. Have a look at the DATE_FORMAT() function.

The main reason is that it avoids the conversion of the date to a Unix
timestamp. These timestamps are much more restricted in range (typically
1970-2038, back to 1902 with negative values) than MySQL timestamps and
should be avoided when they're not really necessary like in this case.

And just in case your script still has to work with Unix timestamps, the
DB can also do this conversion work for you with UNIX_TIMESTAMP(). I
would prefer that instead of a strtotime() call.

Micha
mantrid

2007-10-28, 7:04 pm

thanks Micheal
I will do the formatting in the sql statement as you suggest
Ian


"Michael Fesser" <netizen@gmx.de> wrote in message
news:8d87i3tibgaks4upufavebq6co01611bje@
4ax.com...
> .oO(mantrid)
>
>
> It does, but often it makes more sense to let MySQL return an already
> formatted date. Have a look at the DATE_FORMAT() function.
>
> The main reason is that it avoids the conversion of the date to a Unix
> timestamp. These timestamps are much more restricted in range (typically
> 1970-2038, back to 1902 with negative values) than MySQL timestamps and
> should be avoided when they're not really necessary like in this case.
>
> And just in case your script still has to work with Unix timestamps, the
> DB can also do this conversion work for you with UNIX_TIMESTAMP(). I
> would prefer that instead of a strtotime() call.
>
> Micha



Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com