954,561 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

MySQL date formatting

I just finished a php MySQL query. Everything is great, except
the date column doesn't format correctly.

The date output looks like this: 1175114705

The date column prints from: echo $row['createdTime'];

Ideas? Thanks.

jmueller0823
Newbie Poster
7 posts since Oct 2007
Reputation Points: 10
Solved Threads: 0
 

thats the time. hence "createdTime". thats what the php time() function looks like.

kkeith29
Nearly a Posting Virtuoso
1,357 posts since Jun 2007
Reputation Points: 235
Solved Threads: 194
 

Okay... but isn't there a way to 'convert' that into a standard, readable date format?

jmueller0823
Newbie Poster
7 posts since Oct 2007
Reputation Points: 10
Solved Threads: 0
 

$query="Select MINUTE(date),DAY(date),HOUR(date),
MONTH(date),YEAR(date),WEEK(date),
SECOND(date) from table"

the "date" is the date field of your table.
Try this.thanks.

then combine whatever field of time you want in the output.

ryan_vietnow
Posting Pro
578 posts since Aug 2007
Reputation Points: 28
Solved Threads: 71
 

(new php coder here)

So, if I have something like this

@mysql_query('SELECT DAY(createdTime),MONTH(createdTime),YEAR(createdTime),title FROM...


How would I identify each column?
Normally I would use this: echo $row['column'];
But not sure what to do with the date functions.

Thanks!

jmueller0823
Newbie Poster
7 posts since Oct 2007
Reputation Points: 10
Solved Threads: 0
 
$result = @mysql_query('SELECT createdTime FROM artman_article ');
if (!$result) {
  exit('<p>Error performing query: ' . mysql_error() . '</p>');
}

// Display the text of each rec in a paragraph
while ($row = mysql_fetch_array($result)) {
  echo $row[date('Y m d',strtotime($createdTime))];  
}


After some searching here, it looks like the date() and strtotime()
functions might do the trick.

See above.

But-- Apparently the syntax is incorrect (?). There are no results.
Thanks again.

jmueller0823
Newbie Poster
7 posts since Oct 2007
Reputation Points: 10
Solved Threads: 0
 

I just finished a php MySQL query. Everything is great, except the date column doesn't format correctly.

The date output looks like this: 1175114705

The date column prints from: echo $row['createdTime'];

Ideas? Thanks.

It seems that your timestamp is in unix format. In order to convert it to a humanly readble one there is a function in php called unixtojd(unixdate);

So if it is a unix date, then this code should do the trick:

[php]
$select = 'select timestamp from mydb.mytable';
$rslt = @mysql_query($select);
if (!$rslt)
{
echo 'Ooooppss:'.mysql_error();
}
else {
$unixdate = mysql_fetch_array($rslt);
$normaldate = unixtojd($unixdate['timestamp']);
echo $normaldate;
}
[/php]

Tell us if it works...Note php should be 4 and above

Rhyan
Posting Whiz in Training
240 posts since Oct 2006
Reputation Points: 21
Solved Threads: 26
 

Thanks.
(PHP Version 4.4.7)
Okay. Tried this:

<?php
  
$result = @mysql_query('SELECT createdTime FROM artman_article');
if (!$result) {
  exit('<p>Error performing query: ' . mysql_error() . '</p>');
}

// Display each rec
$unixdate= mysql_fetch_array($result);
$normaldate = unixtojd($unixdate['createdTime']);
echo $normaldate;
   
}

?>

No results. No errors. Ideas?
(The basic code structure is okay i.e. using different columns in Select,
the code will produce results. The above is just an excerpt from the script.)

jmueller0823
Newbie Poster
7 posts since Oct 2007
Reputation Points: 10
Solved Threads: 0
 

Thanks. (PHP Version 4.4.7) Okay. Tried this:

<?php
  
$result = @mysql_query('SELECT createdTime FROM artman_article');
if (!$result) {
  exit('<p>Error performing query: ' . mysql_error() . '</p>');
}

// Display each rec
$unixdate= mysql_fetch_array($result);
$normaldate = unixtojd($unixdate['createdTime']);
echo $normaldate;
   
}

?>
No results. No errors. Ideas? (The basic code structure is okay i.e. using different columns in Select, the code will produce results. The above is just an excerpt from the script.)

Is that the complete code you have entered? If it is so, I don't see your loging into mysql. I don't need to see that, however, first login into mysql, then perform the query.

it should be like this:

@$connect=mysql_connect('host', 'user', 'pass');
if (!$connect){echo 'Login failed'.mysql_error();}
@$select = 'SELECT createdTime FROM artman_article';
@$result = mysql_query($select, $connect); /*$select is the query statement, and $connect is the connection identifier, so the mysql_query will use the correct connection.*/ 
if (!$result) {
  die('<p>Error performing query: '. mysql_error().'</p>');
}

// Display each rec
$unixdate= mysql_fetch_array($result);
$normaldate = unixtojd($unixdate['createdTime']); /* or try $normaldate = unixtojd($unixdate[0]); in case the mysql_fetch_array has returned a number indexed array. */
echo $normaldate;



I think it should be like this

Rhyan
Posting Whiz in Training
240 posts since Oct 2006
Reputation Points: 21
Solved Threads: 26
 


Unable to connect to the ' .
'database server at this time.');
}

// Select the database
if (!@mysql_select_db('grow')) {
exit('Unable to locate the ' .
'database at this time.


');
}

?>
Featured Article Candidates

<?php

$result = @mysql_query('SELECT createdTime FROM artman_article'); if (!$result) { exit('

Error performing query: ' . mysql_error() . '

'); }

// Display text $unixdate= mysql_fetch_array($result); $normaldate = unixtojd($unixdate[0]); /* or try $normaldate = unixtojd($unixdate[0]); in case the mysql_fetch_array has returned a number indexed array. */ echo $normaldate;

}

?>



jmueller0823
Newbie Poster
7 posts since Oct 2007
Reputation Points: 10
Solved Threads: 0
 

SOLVED

Simply: FROM_UNIXTIME()

jmueller0823
Newbie Poster
7 posts since Oct 2007
Reputation Points: 10
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You