Click to See Complete Forum and Search --> : [Resolved] was there a better way to flip this date?


jasonmills58
01-17-2004, 06:19 AM
So, I'm getting my date from MySQL in the yyyy-mm-dd format into the variable $a_row['date'] and I wanted to display it as Jan 15, 2004 so I did this:

$date = explode ( "-", $a_row['date'] );
$year = $date[0];
$month = $date[1];
$day = $date[2];
$ts = mktime( 0, 0, 0, $month, $day, $year );
$date = date( "F j Y", $ts );
$date = explode( " ", $date );
$month = substr( $date[0], 0, 3 );
$date = $month." ".$date[1].", ".$date[2];
print $date;

It works perfectly. First try, even! *pulls muscle patting self on back* :D I was just wondering if anyone could think of a more concise way of doing this.

I know I could change this:
$year = $date[0];
$month = $date[1];
$day = $date[2];
$ts = mktime( 0, 0, 0, $month, $day, $year );
to:
$ts = mktime( 0, 0, 0, $date[1], $date[2], $date[0] );
but I guess i liked the clarity of $ts = mktime( 0, 0, 0, $month, $day, $year );

cosminb
01-17-2004, 06:21 AM
strtotime (http://www.php.net/strtotime) & date (http://www.php.net/date)

jasonmills58
01-17-2004, 06:28 AM
yes, thank you. Unfortunately strtotime() does not know what to do with 2004-01-18. And in regards to date(), I believe I used it correctly in my example, but I am of course open to critiques...

thanks again.

cosminb
01-17-2004, 06:34 AM
Originally posted by jasonmills58
... Unfortunately strtotime() does not know what to do with 2004-01-18. ...
what are you talking about ???.
try this:

echo date('r', strtotime('2004-01-18'));

cosminb
01-17-2004, 06:36 AM
... so I guess all your 9 lines of code could be replaced by one:

print date('F j Y', strtotime($a_row['date']));

LordShryku
01-17-2004, 11:49 AM
You could also use date_format (http://www.mysql.com/doc/en/Date_and_time_functions.html) in your SQL

jasonmills58
01-17-2004, 11:43 PM
Thanks for the ideas guys. I think like how mine has it all layed out. I get too easily confused... :D

And I can't use "print date('F j Y', strtotime($a_row['date']));" because the whole reason i did this was to have three letter month names like Jan, Feb, etc ( pretty lame, huh? ).

cheers!
jason

Moonglobe
01-18-2004, 01:19 AM
then use the "M" modifier of date

jasonmills58
01-18-2004, 02:10 AM
o.o -.- o.o -.- o.o :eek:

but I looked for that. I could have sworn it wasn't there. I thought to myself, "Self, there has to be a month abbreviator." but I couldn't find it. and it was right there the whole time.

*is most embarrased* but thank you moonglobe, i appreciate it.

so, is there a way to tell PHP that $a_row['eventdate'] is populated with a MySQL date value to shorten this:

$date = explode ( "-", $a_row['eventdate'] );
$year = $date[0];
$month = $date[1];
$day = $date[2];
$ts = mktime( 0, 0, 0, $month, $day, $year );
$date = date( "M j, Y", $ts );

Weedpacket
01-23-2004, 07:18 AM
Originally posted by jasonmills58
"Self, there has to be a month abbreviator." but I couldn't find it. and it was right there the whole time. I was thinking "Who in their right mind has a use for 'B'?"

so, is there a way to tell PHP that $a_row['eventdate'] is populated with a MySQL date value to shorten this:Well, that's what [this] is doing :). Here are three suggestions:
I tend to use sscanf() and list() rather than explode() and all that. YYYY-MM-DD is a format that is recognised by strtotime(). You can have MySQL format the date to your specification before returning it with its own date formatting function.