User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the PHP section within the Web Development category of DaniWeb, a massive community of 456,528 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,781 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our PHP advertiser: Lunarpages PHP Web Hosting
Views: 1969 | Replies: 10 | Solved
Reply
Join Date: Oct 2007
Posts: 7
Reputation: jmueller0823 is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
jmueller0823 jmueller0823 is offline Offline
Newbie Poster

MySQL date formatting

  #1  
Oct 2nd, 2007
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.
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Jun 2007
Location: Valley Center, Kansas
Posts: 643
Reputation: kkeith29 is on a distinguished road 
Rep Power: 3
Solved Threads: 72
kkeith29's Avatar
kkeith29 kkeith29 is offline Offline
Practically a Master Poster

Re: MySQL date formatting

  #2  
Oct 2nd, 2007
thats the time. hence "createdTime". thats what the php time() function looks like.
Reply With Quote  
Join Date: Oct 2007
Posts: 7
Reputation: jmueller0823 is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
jmueller0823 jmueller0823 is offline Offline
Newbie Poster

Re: MySQL date formatting

  #3  
Oct 2nd, 2007
Okay... but isn't there a way to 'convert' that into a standard, readable date format?
Reply With Quote  
Join Date: Aug 2007
Location: Cavite,Philippines
Posts: 508
Reputation: ryan_vietnow is an unknown quantity at this point 
Rep Power: 3
Solved Threads: 68
ryan_vietnow's Avatar
ryan_vietnow ryan_vietnow is offline Offline
Posting Pro

Re: MySQL date formatting

  #4  
Oct 2nd, 2007
$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.
"death is the cure of all diseases..."
http://ryantetek.wordpress.com
Reply With Quote  
Join Date: Oct 2007
Posts: 7
Reputation: jmueller0823 is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
jmueller0823 jmueller0823 is offline Offline
Newbie Poster

Re: MySQL date formatting

  #5  
Oct 2nd, 2007
(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!
Reply With Quote  
Join Date: Oct 2007
Posts: 7
Reputation: jmueller0823 is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
jmueller0823 jmueller0823 is offline Offline
Newbie Poster

Re: MySQL date formatting

  #6  
Oct 2nd, 2007
$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.
Last edited by jmueller0823 : Oct 2nd, 2007 at 1:07 pm.
Reply With Quote  
Join Date: Oct 2006
Location: Sofia, Bulgaria
Posts: 181
Reputation: Rhyan is an unknown quantity at this point 
Rep Power: 3
Solved Threads: 10
Rhyan's Avatar
Rhyan Rhyan is online now Online
Junior Poster

Re: MySQL date formatting

  #7  
Oct 2nd, 2007
Originally Posted by jmueller0823 View Post
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
" Of all the things I've lost,
I miss my mind the most...."
Mark Twain
Reply With Quote  
Join Date: Oct 2007
Posts: 7
Reputation: jmueller0823 is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
jmueller0823 jmueller0823 is offline Offline
Newbie Poster

Re: MySQL date formatting

  #8  
Oct 2nd, 2007
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.)
Last edited by jmueller0823 : Oct 2nd, 2007 at 2:54 pm.
Reply With Quote  
Join Date: Oct 2006
Location: Sofia, Bulgaria
Posts: 181
Reputation: Rhyan is an unknown quantity at this point 
Rep Power: 3
Solved Threads: 10
Rhyan's Avatar
Rhyan Rhyan is online now Online
Junior Poster

Re: MySQL date formatting

  #9  
Oct 2nd, 2007
Originally Posted by jmueller0823 View Post
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
" Of all the things I've lost,
I miss my mind the most...."
Mark Twain
Reply With Quote  
Join Date: Oct 2007
Posts: 7
Reputation: jmueller0823 is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
jmueller0823 jmueller0823 is offline Offline
Newbie Poster

Re: MySQL date formatting

  #10  
Oct 2nd, 2007
Nope. Tried $normaldate = unixtojd($unixdate[0]); also.
Returning a blank screen.

This is what I have. The Select and Display portions should
be the same as your example.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Featured Article Candidates</title>
<meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />
</head>
<body>
<font
<?php

// Connect to the db server
$dbcnx = @mysql_connect('localhost', 'user', 'password');
if (!$dbcnx) {
exit('<p>Unable to connect to the ' .
'database server at this time.</p>');
}

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

?>
<p>Featured Article Candidates</p>
<blockquote>
<?php


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

// 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;

}

?>
</blockquote>
</font>
</body>
</html>

Again, this script does work with different columns. Thanks again for the assist.
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb PHP Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the PHP Forum

All times are GMT -4. The time now is 4:18 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC