To register for an Internet.com membership to receive newsletters and white papers, use the Register button ABOVE.
To participate in the message forums BELOW, click here
PHPBuilder.com  
 

 

Go Back   PHPBuilder.com > PHP Help > Code Critique

Code Critique Having someone critique your code is always a great way to hone the skills. Stop in and post your code to see what your peers may have done differently.

Reply
 
Thread Tools Rate Thread Display Modes
Old 01-17-2004, 06:19 AM   #1
jasonmills58
Alien Intelligence
 
jasonmills58's Avatar
 
Join Date: Dec 2003
Location: <clouds>head</clouds>
Posts: 292
was there a better way to flip this date?

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:

PHP Code:
$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* I was just wondering if anyone could think of a more concise way of doing this.

I know I could change this:
PHP Code:
$year = $date[0];
$month = $date[1];
$day = $date[2];
$ts = mktime( 0, 0, 0, $month, $day, $year );
to:
PHP Code:
$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 );
__________________
Nice little planet ya got here. Be a shame if somethin' happened to it...
jasonmills58 is offline   Reply With Quote
Old 01-17-2004, 06:21 AM   #2
cosminb
Senior Member
 
cosminb's Avatar
 
Join Date: May 2003
Location: /dev/null/
Posts: 284
strtotime & date
__________________
Why waste time reinventing the wheel, when you could be reinventing the engine?

>>> Web Developer
cosminb is offline   Reply With Quote
Old 01-17-2004, 06:28 AM   #3
jasonmills58
Alien Intelligence
 
jasonmills58's Avatar
 
Join Date: Dec 2003
Location: <clouds>head</clouds>
Posts: 292
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.
__________________
Nice little planet ya got here. Be a shame if somethin' happened to it...
jasonmills58 is offline   Reply With Quote
Old 01-17-2004, 06:34 AM   #4
cosminb
Senior Member
 
cosminb's Avatar
 
Join Date: May 2003
Location: /dev/null/
Posts: 284
Quote:
Originally posted by jasonmills58
... Unfortunately strtotime() does not know what to do with 2004-01-18. ...
what are you talking about ???.
try this:
PHP Code:
echo date('r', strtotime('2004-01-18'));
__________________
Why waste time reinventing the wheel, when you could be reinventing the engine?

>>> Web Developer
cosminb is offline   Reply With Quote
Old 01-17-2004, 06:36 AM   #5
cosminb
Senior Member
 
cosminb's Avatar
 
Join Date: May 2003
Location: /dev/null/
Posts: 284
... so I guess all your 9 lines of code could be replaced by one:
PHP Code:
print date('F j Y', strtotime($a_row['date']));
__________________
Why waste time reinventing the wheel, when you could be reinventing the engine?

>>> Web Developer
cosminb is offline   Reply With Quote
Old 01-17-2004, 11:49 AM   #6
LordShryku
kung foo code monkey
 
LordShryku's Avatar
 
Join Date: Aug 2002
Location: Occupational Hypnotherapy
Posts: 7,473
You could also use date_format in your SQL
LordShryku is offline   Reply With Quote
Old 01-17-2004, 11:43 PM   #7
jasonmills58
Alien Intelligence
 
jasonmills58's Avatar
 
Join Date: Dec 2003
Location: <clouds>head</clouds>
Posts: 292
Thanks for the ideas guys. I think like how mine has it all layed out. I get too easily confused...

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
__________________
Nice little planet ya got here. Be a shame if somethin' happened to it...

Last edited by jasonmills58; 01-18-2004 at 12:18 AM.
jasonmills58 is offline   Reply With Quote
Old 01-18-2004, 01:19 AM   #8
Moonglobe
Better fan than rebelo!
 
Moonglobe's Avatar
 
Join Date: Apr 2003
Location: brain://localhost:left-side
Posts: 2,381
then use the "M" modifier of date
__________________
there's no place i can be, since i found serenity.
Moonglobe is offline   Reply With Quote
Old 01-18-2004, 02:10 AM   #9
jasonmills58
Alien Intelligence
 
jasonmills58's Avatar
 
Join Date: Dec 2003
Location: <clouds>head</clouds>
Posts: 292
o.o -.- o.o -.- o.o

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:

PHP Code:
$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 );
__________________
Nice little planet ya got here. Be a shame if somethin' happened to it...

Last edited by jasonmills58; 01-18-2004 at 02:17 AM.
jasonmills58 is offline   Reply With Quote
Old 01-23-2004, 07:18 AM   #10
Weedpacket
Custom User Title™
 
Weedpacket's Avatar
 
Join Date: Aug 2002
Location: Rapid Offensive Unit "Foreign Object Damage"
Posts: 19,128
Quote:
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'?"

Quote:
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:
  1. I tend to use sscanf() and list() rather than explode() and all that.
  2. YYYY-MM-DD is a format that is recognised by strtotime().
  3. You can have MySQL format the date to your specification before returning it with its own date formatting function.
__________________
On two occasions I have been asked [by Members of Parliament], "Pray, Mr. Babbage, if you put into the machine wrong figures, will the right answers come out?" I am not able rightly to apprehend the kind of confusion of ideas that could provoke such a question.
Weedpacket is offline   Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Forum Jump


All times are GMT -4. The time now is 08:30 AM.






Acceptable Use Policy

internet.comMediabistrojusttechjobs.comGraphics.com

WebMediaBrands Corporate Info


Advertise | Newsletters | Feedback | Submit News

Legal Notices | Licensing | Permissions | Privacy Policy


Powered by vBulletin® Version 3.7.2
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.