Jump to content

Convert MySQL 14 digit timestamp into Readable format


mjurmann

Recommended Posts

Hello. I am trying to convert a 14 digit timestamp located in a field in my MySQL database. This is the format I'm using right now to convert it to a readable format: <?php echo date("D M j G:i:s T Y",$row_Recordset4['date_posted']);?> I'm getting Mon Jan 18 22:14:07 EST 2038 as the result, which is not correct. Can someone please tell me why I'm getting an incorrect time (the year 2038? come on!) and how I can fix this.

 

I've been combing google and these forums all night and can't figure it out...

 

Thanks much

Link to comment
Share on other sites

The PHP date function take a string format and a unix timestamp.

 

Unix timestamp is an integer, ie.  1342385029.

The 'date_posted' retrieved from mysql is the format of mysql, probably human readable like "2007-02-04 10:40:05 am".  You can not use this and passed into php date function and expect it to work right.

 

you have to convert that mysql time stamp into unix timestamp in order to for php's date to work.

Take a look at php's strtotime function.

 

Link to comment
Share on other sites

I don't understand something :), you have 14 digit number with out any separators, right. And how you get this number? Is it in any format or is's just 2003-01-01 00:00:00 -> 20030101000000 ;)? Because if you make it in this way you can separate it with php functions.

Link to comment
Share on other sites

(2038 is the default last year that I have been able to test scripts through, so  it souns like its defaulting to the end)

 

Try adding it to the query, such as:

 

"SELECT  *, date_format(begindate, '%m/%d/%Y'), date_format(enddate, '%m/%d/%Y') FROM road"

 

Then, echo back: <?php  echo $row["date_format(begindate, '%m/%d/%Y')"]; ?>

 

This works with the UNIX 14 digit timestamp.

 

Alternately, you can my convoluted method (works create with many calendar scripts if you bring the date field in for editing):

 

$daystring = $row["begindate"];

$month = substr($daystring, 4, 2);

$day = substr($daystring, 6, 2);

$year = substr($daystring, 0, 4);

THEN:

<?php echo $month,"-",$day,"-",$year ?>

 

I hope this works for you.  :)

Link to comment
Share on other sites

maybe i could help i have had this error b4 try this code and tell my wat u get out of it exactly:

 

<?php
$news = mysql_query("SELECT * FROM news WHERE id= 1"); 
$newsarr = mysql_fetch_array($news);
echo $newsarr[date];
?>

replace 1 with a valid id!!!

replace date with the correct value like whatever the the date is stored as!!!

 

Then reply if u get the same thing as i did then i will tell u from there :)

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.