MySQL Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
Go Back   Dev Articles Community ForumsDatabasesMySQL Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old January 3rd, 2004, 08:58 PM
banx banx is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Feb 2003
Posts: 33 banx User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 6
I'm having a lot of trouble formatting the date in MySQL. Can someone help?

Hi,

I'm having problems with my date feature on a news board I'm using. I'm trying to get the date a given article is posted.

Right now the date is featured like this:
20040103190226


In this script, I get that by using this:
<?php echo $row["datePublished"]; ?>


I would like to format the date like this, January 3, 2004.

I tried this:
<?php echo $row["datePublished"]("F j, Y, g:i a"); ?>

But I only get this error message:
Fatal error: Call to undefined function: 20040103190226()

Any suggestions?

Thanks

Reply With Quote
  #2  
Old January 3rd, 2004, 09:08 PM
MadCowDzz's Avatar
MadCowDzz MadCowDzz is offline
I'm Internet Famous
Dev Articles Frequenter (2500 - 2999 posts)
 
Join Date: Jan 2003
Location: Toronto, Canada
Posts: 2,890 MadCowDzz User rank is Lance Corporal (50 - 100 Reputation Level)MadCowDzz User rank is Lance Corporal (50 - 100 Reputation Level)MadCowDzz User rank is Lance Corporal (50 - 100 Reputation Level) 
Time spent in forums: 1 Week 16 h 14 m 9 sec
Reputation Power: 8
banx,

There's two ways to approach this... you can either alter your SQL statement to return the desired date, or convert your display data via PHP... either way is generally okay... Here's a couple examples to acheive January 4, 2004

try this:
<?php echo date("F j, Y", $row['datePublished']); ?>

or this:
SELECT DATE_FORMAT(datefield, '%M %e, %Y') FROM table


Please note, I haven't tested either examples, but i'm pretty positive they'll work...

Refer to the respective documention:
http://www.php.net/date
http://www.mysql.com/doc/en/Date_an..._functions.html

Reply With Quote
  #3  
Old January 3rd, 2004, 11:08 PM
banx banx is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Feb 2003
Posts: 33 banx User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 6
I used :
<?php echo date("F j, Y", $row['datePublished']); ?>


But all I got was:
January 18, 2038

Which is the right format, but not the date, and its the same date on all news posts.

Reply With Quote
  #4  
Old January 4th, 2004, 12:11 AM
MadCowDzz's Avatar
MadCowDzz MadCowDzz is offline
I'm Internet Famous
Dev Articles Frequenter (2500 - 2999 posts)
 
Join Date: Jan 2003
Location: Toronto, Canada
Posts: 2,890 MadCowDzz User rank is Lance Corporal (50 - 100 Reputation Level)MadCowDzz User rank is Lance Corporal (50 - 100 Reputation Level)MadCowDzz User rank is Lance Corporal (50 - 100 Reputation Level) 
Time spent in forums: 1 Week 16 h 14 m 9 sec
Reputation Power: 8
What's the column type of the data in Mysql?
have you tried just outputting the data directly?

Or even just a simple:
<?php echo date("F j, Y")." **** ".$row['datePublished']; ?>

This will help debug it... it should display the current date in your format, then whatever data is in the particular date field of the row... seperated by asterisks so you see it easier =)

Reply With Quote
  #5  
Old January 4th, 2004, 11:04 PM
banx banx is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Feb 2003
Posts: 33 banx User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 6
The column type is "timestamp(14)".

How would I go about outputting the data directly? I'm not too good at all with MySQL/PHP so I wouldn't really know.

Quote:
<?php echo date("F j, Y")." **** ".$row['datePublished']; ?>



What are the "****" supposed to be?


Also, I forgot to add. Its a news script so I'm trying to get the date of when the news report was posted. I can get them on them on there but its all stuck together, like this...20040104823. I'm just trying to figure out to how to make it appear a different way.

Reply With Quote
  #6  
Old January 5th, 2004, 12:22 AM
MadCowDzz's Avatar
MadCowDzz MadCowDzz is offline
I'm Internet Famous
Dev Articles Frequenter (2500 - 2999 posts)
 
Join Date: Jan 2003
Location: Toronto, Canada
Posts: 2,890 MadCowDzz User rank is Lance Corporal (50 - 100 Reputation Level)MadCowDzz User rank is Lance Corporal (50 - 100 Reputation Level)MadCowDzz User rank is Lance Corporal (50 - 100 Reputation Level) 
Time spent in forums: 1 Week 16 h 14 m 9 sec
Reputation Power: 8
I put the stars in just so you could see the difference in the two peices of data... I usually forget and all my variables blend together... they serve no purpose, just for display

Is the data in your table correct? what happens if you simply output $row['datepublished']?

Reply With Quote
  #7  
Old January 5th, 2004, 01:34 AM
banx banx is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Feb 2003
Posts: 33 banx User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 6
If I put this:

<?php echo date("F j, Y", $row['datePublished']); ?>

I get today's date followed by this: "20040104823"

I tried this:
<?php echo $row["datePublished"]("F j, Y, g:i a"); ?>

But I only get this error message:
Fatal error: Call to undefined function: 20040103190226()


When I use:
<?php echo $row["datePublished"]; ?>

I just get this:
20040103190226

Is that what you meant?

Reply With Quote
  #8  
Old January 5th, 2004, 08:13 AM
dhouston's Avatar
dhouston dhouston is offline
Contributing User
Dev Articles Beginner (1000 - 1499 posts)
 
Join Date: May 2003
Location: Tennessee
Posts: 1,355 dhouston User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 7
Send a message via ICQ to dhouston
You might need to wrap UNIX_TIMESTAMP() around your datePublished field in your query to get it into an 11-digit unix timestamp, which is what the date() function requires.

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsDatabasesMySQL Development > I'm having a lot of trouble formatting the date in MySQL. Can someone help?


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 1 hosted by Hostway
Stay green...Green IT