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

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 12-10-2012, 05:39 PM   PM User | #1
elitis
Regular Coder

 
Join Date: Sep 2010
Posts: 319
Thanks: 9
Thanked 6 Times in 6 Posts
elitis is an unknown quantity at this point
Date Format gone wrong

It is displaying the wrong date after being echoed but is fine when just the date is echoed as well as in the database.
PHP Code:
$date $row['date'];
echo 
"<p><b>" $row['review'] . "</b></p><p>" date("F jS, Y g:i A"$date) . "</p><h1 class='centered'>Comments</h1></article>"
__________________
Coding is a challenge, get used to it
Always remember to debug
Try the guess & check method
Break it down into simple steps

Last edited by elitis; 12-10-2012 at 06:38 PM..
elitis is offline   Reply With Quote
Old 12-10-2012, 06:00 PM   PM User | #2
LearningCoder
Regular Coder

 
LearningCoder's Avatar
 
Join Date: Jan 2011
Location: The Pleiades
Posts: 849
Thanks: 67
Thanked 28 Times in 28 Posts
LearningCoder is an unknown quantity at this point
What format is the date stored in the database? Is it just a timestamp?

Regards,

LC.
LearningCoder is offline   Reply With Quote
Old 12-10-2012, 06:05 PM   PM User | #3
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,661
Thanks: 4
Thanked 2,452 Times in 2,421 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
Sounds to me that its stored in a date/datetime datatype if you can view it directly from pulling a db record.
That means your result of calling the date function will result in either December 31, 1969 or January 1, 1970 depending on your timezone (its currently at unix epoch).
Therefore you cannot use the date() function directly as it requires an integer timestamp. Your options are either to:
  1. Format the date in the query
  2. Use the UNIX_TIMESTAMP function in MySQL to convert it to a timestamp
  3. Split the parts up and use the mktime function
  4. Run the result through an strtotime or DateTime object to convert it to an appropriate timestamp. Then use date or DateTime::format on it.
Fou-Lu is offline   Reply With Quote
Old 12-10-2012, 06:19 PM   PM User | #4
elitis
Regular Coder

 
Join Date: Sep 2010
Posts: 319
Thanks: 9
Thanked 6 Times in 6 Posts
elitis is an unknown quantity at this point
Quote:
Originally Posted by LearningCoder View Post
What format is the date stored in the database? Is it just a timestamp?

Regards,

LC.
Quote:
Originally Posted by Fou-Lu View Post
Sounds to me that its stored in a date/datetime datatype if you can view it directly from pulling a db record.
That means your result of calling the date function will result in either December 31, 1969 or January 1, 1970 depending on your timezone (its currently at unix epoch).
Therefore you cannot use the date() function directly as it requires an integer timestamp. Your options are either to:
  1. Format the date in the query
  2. Use the UNIX_TIMESTAMP function in MySQL to convert it to a timestamp
  3. Split the parts up and use the mktime function
  4. Run the result through an strtotime or DateTime object to convert it to an appropriate timestamp. Then use date or DateTime::format on it.
It is stored in a timestamp format, but is still displaying December 31, 1969 4:33 PM
EDIT:Still not sure what was the issue but got it displaying the correct date by calling it through the strotime() function.
__________________
Coding is a challenge, get used to it
Always remember to debug
Try the guess & check method
Break it down into simple steps

Last edited by elitis; 12-10-2012 at 06:37 PM..
elitis is offline   Reply With Quote
Old 12-10-2012, 07:22 PM   PM User | #5
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,661
Thanks: 4
Thanked 2,452 Times in 2,421 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
Quote:
Originally Posted by elitis View Post
It is stored in a timestamp format, but is still displaying December 31, 1969 4:33 PM
EDIT:Still not sure what was the issue but got it displaying the correct date by calling it through the strotime() function.
Yeah this is where it gets confusing.
Timestamp in MySQL is a [more or less] datetime type that has been given an explicit range (from epoch to 32bit max [ie: January 19, 2038 @ 03:14:07; the real year 2000]). So its represented internally as a number, while datetime would be a string. When passed to PHP, it is a string, not a number.

Timestamp in PHP is an integer. Using the UNIX_TIMESTAMP returns the integer timestamp result of the MySQL TIMESTAMP so that PHP can make use of it as a $timestamp in its date functions.
Fou-Lu 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 01:22 AM.


Advertisement
Log in to turn off these ads.