Forum Moderators: coopster

Message Too Old, No Replies

Date conversion question

         

StoutFiles

3:59 pm on Mar 3, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



This is probably simple but date conversion with the php date functions still confuses me. I need the value

2010-03-02 06:33:00.00

converted to

02-Mar-2010 06:33:00

Any ideas?

Readie

4:29 pm on Mar 3, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



$datetime = '2010-03-02 06:33:00.00';
$datetime = explode(' ',$datetime);
$date_pieces = explode('-',$datetime[0]);
$date = date('jS-M-Y',mktime(0,0,0,$date_pieces,$date_pieces[2],$date_pieces[0]));
$datetime = explode('.',$datetime[1]);
echo $date . $datetime[0];

Should be what you're after

Edit: Fixed a typo

[1][edited by: Readie at 5:25 pm (utc) on Mar 3, 2010]

StoutFiles

4:58 pm on Mar 3, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Changed jS to d. Works great though, thank you.

rocknbil

7:34 pm on Mar 3, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



mySQL already has this, no need to add coding where you don't need it. :-) Even if the value is not extracted from a DB (unlikely it isn't, considering your example) you can still execute mySQL on any value.

select date_format('2010-03-02 06:33:00.00',"%d-%b-%Y %l:%i:%s");

--> 02-Mar-2010 06:33:00

To execute it as you select from a table,

select fname, lname, whatever, date_format(date_time_field,"%d-%b-%Y %l:%i:%s") from table;

Documentation [dev.mysql.com]

g1smd

8:15 pm on Mar 3, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



If that's conversion for display, do consider using the Big-Endian format for that too.

It's easier to read long columns of Big-Endian formatted dates, and an alpha sort (with numeric month) sorts them into date order.

See also RFC 3339.

StoutFiles

8:32 pm on Mar 3, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



mySQL already has this, no need to add coding where you don't need it. :-) Even if the value is not extracted from a DB (unlikely it isn't, considering your example) you can still execute mySQL on any value.


Using Oracle 7 for VMS. I normally don't consider query functions since they're so limited.