Go Back   CodingForums.com > :: Server side development > MySQL

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 04-23-2012, 05:50 AM   PM User | #1
Name
New Coder

 
Join Date: Apr 2010
Posts: 53
Thanks: 3
Thanked 0 Times in 0 Posts
Name is an unknown quantity at this point
Date formatting

I don't know if this is PHP or SQL formatting, so forgive me.

Problem:
Code:
  echo "<td>" . date('Y-m-d', $row['EntryDate']) . "</td>";
Result:
Code:
1969-12-31
Raw:
Code:
2012-04-22 21:55:01
Desired:
Code:
April 22, 2012

Last edited by Name; 04-24-2012 at 04:05 AM..
Name is offline   Reply With Quote
Old 04-23-2012, 07:12 AM   PM User | #2
Shane Mark
New to the CF scene

 
Join Date: Mar 2012
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Shane Mark is an unknown quantity at this point
From last many time i found date format for database finally i got that. Thank you...
Shane Mark is offline   Reply With Quote
Old 04-23-2012, 09:36 AM   PM User | #3
felgall
Master Coder

 
felgall's Avatar
 
Join Date: Sep 2005
Location: Sydney, Australia
Posts: 5,532
Thanks: 0
Thanked 503 Times in 494 Posts
felgall is a jewel in the roughfelgall is a jewel in the roughfelgall is a jewel in the rough
Quote:
Originally Posted by Name View Post
Problem:
Code:
echo "<td>" . date('Y-m-d', $row['EntryDate']) . "</td>";
Solution
Code:
echo "<td>" . date('F j, Y', $row['EntryDate']) . "</td>";
__________________
Stephen
Learn Modern JavaScript - http://javascriptexample.net/
Helping others to solve their computer problem at http://www.felgall.com/
felgall is offline   Reply With Quote
Old 04-23-2012, 12:32 PM   PM User | #4
Name
New Coder

 
Join Date: Apr 2010
Posts: 53
Thanks: 3
Thanked 0 Times in 0 Posts
Name is an unknown quantity at this point
Quote:
Originally Posted by felgall View Post
Solution
Code:
echo "<td>" . date('F j, Y', $row['EntryDate']) . "</td>";
Result:
Code:
December 31, 1969
Name is offline   Reply With Quote
Old 04-23-2012, 01:19 PM   PM User | #5
abduraooft
Supreme Master coder!

 
abduraooft's Avatar
 
Join Date: Mar 2007
Location: N/A
Posts: 14,689
Thanks: 158
Thanked 2,184 Times in 2,171 Posts
abduraooft is just really niceabduraooft is just really niceabduraooft is just really niceabduraooft is just really niceabduraooft is just really nice
What do you get when you echo $row['EntryDate'] ?
__________________
Quote:
The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)
abduraooft is offline   Reply With Quote
Old 04-23-2012, 10:44 PM   PM User | #6
Name
New Coder

 
Join Date: Apr 2010
Posts: 53
Thanks: 3
Thanked 0 Times in 0 Posts
Name is an unknown quantity at this point
Quote:
Originally Posted by abduraooft View Post
what do you get when you echo $row['entrydate'] ?
Code:
2012-04-22 21:55:01
Name is offline   Reply With Quote
Old 04-23-2012, 10:57 PM   PM User | #7
felgall
Master Coder

 
felgall's Avatar
 
Join Date: Sep 2005
Location: Sydney, Australia
Posts: 5,532
Thanks: 0
Thanked 503 Times in 494 Posts
felgall is a jewel in the roughfelgall is a jewel in the roughfelgall is a jewel in the rough
Oops - left out a small piece of the code:

Code:
echo "<td>" . date('F j, Y', DateTime($row['EntryDate'])) . "</td>";
You need to convert the text string returned to a date before you can format it.
__________________
Stephen
Learn Modern JavaScript - http://javascriptexample.net/
Helping others to solve their computer problem at http://www.felgall.com/
felgall is offline   Reply With Quote
Old 04-24-2012, 12:24 AM   PM User | #8
guelphdad
Super Moderator


 
guelphdad's Avatar
 
Join Date: Mar 2006
Location: St. Catharines, Ontario Canada
Posts: 2,629
Thanks: 4
Thanked 147 Times in 138 Posts
guelphdad will become famous soon enoughguelphdad will become famous soon enough
why not format the date using DATE_FORMAT directly in your mysql query, then there is no need to do it at output in the php code.
guelphdad is offline   Reply With Quote
Old 04-24-2012, 02:34 AM   PM User | #9
Name
New Coder

 
Join Date: Apr 2010
Posts: 53
Thanks: 3
Thanked 0 Times in 0 Posts
Name is an unknown quantity at this point
Quote:
Originally Posted by guelphdad View Post
why not format the date using DATE_FORMAT directly in your mysql query, then there is no need to do it at output in the php code.
I would like to keep the time of the entry, however format the result. Would it be better to have 2 fields? One for date. One for time?
Name is offline   Reply With Quote
Old 04-24-2012, 03:53 AM   PM User | #10
Name
New Coder

 
Join Date: Apr 2010
Posts: 53
Thanks: 3
Thanked 0 Times in 0 Posts
Name is an unknown quantity at this point
Quote:
Originally Posted by felgall View Post
Oops - left out a small piece of the code:

Code:
echo "<td>" . date('F j, Y', DateTime($row['EntryDate'])) . "</td>";
You need to convert the text string returned to a date before you can format it.
This broke the query and it doesn't show any data in the table.
Name is offline   Reply With Quote
Old 04-24-2012, 04:04 AM   PM User | #11
Name
New Coder

 
Join Date: Apr 2010
Posts: 53
Thanks: 3
Thanked 0 Times in 0 Posts
Name is an unknown quantity at this point
Code:
echo "<td>" . date('D M j, Y', strtotime($row['EntryDate'])) . "</td>";
Here is the solution. Was missing the strtotime function.
Name is offline   Reply With Quote
Old 04-24-2012, 04:06 AM   PM User | #12
guelphdad
Super Moderator


 
guelphdad's Avatar
 
Join Date: Mar 2006
Location: St. Catharines, Ontario Canada
Posts: 2,629
Thanks: 4
Thanked 147 Times in 138 Posts
guelphdad will become famous soon enoughguelphdad will become famous soon enough
Perhaps you should look up DATE_FORMAT in the MySQL manual.
guelphdad is offline   Reply With Quote
Old 04-24-2012, 04:32 AM   PM User | #13
Name
New Coder

 
Join Date: Apr 2010
Posts: 53
Thanks: 3
Thanked 0 Times in 0 Posts
Name is an unknown quantity at this point
Date() worked though?
Name is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
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 On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 04:25 AM.


Advertisement
Log in to turn off these ads.