GIDForums  

Go Back   GIDForums > Computer Programming Forums > MySQL / PHP Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 16-Mar-2004, 12:51
cave monkey cave monkey is offline
New Member
 
Join Date: Mar 2004
Location: Juneau, Alaska
Posts: 7
cave monkey is on a distinguished road

Rookie problem with date format


Help please. I am very new to PHP and mysql, and am trying to write a simple script to display tide tables for my area. My problem is this: My tides data in the database uses the default mysql time format (ex. 22:17:32) and I would like the output of my query to be displayed as 10:17 PM (with no seconds). My query is executed as "Select * from `tides` where `date` = NOW()" and works fine except for the time format. I have played with DATE_FORMAT() as described on the MySQL homepage and in the article on this site, but can't get the query to execute as I assume it has to mach the data in the database. Maybe DATE_FORMAT() would work when querying for a specifc date, but not sure how to implement it with SELECT * and the variable NOW(). Should I be affecting the output via PHP date formats or MySQL date formats? Thanks for the help and the patience. DM
  #2  
Old 17-Mar-2004, 04:11
JdS's Avatar
JdS JdS is offline
Senior Member
 
Join Date: Aug 2001
Location: KUL, Malaysia
Posts: 3,371
JdS will become famous soon enough
Hello cave monkey,

Basically, you can use DATE_FORMAT() for this but just in case I've misunderstood your question, you should also try using TIME_FORMAT().

Here's an example:

PHP Code:

<?php
// suppose the column you want to format is in the TIME column type
// so, a sample formatting could look like this:
$sql  =  'SELECT TIME_FORMAT(`date`, \'%h:%I%p"\') AS `tide_time`
         WHERE `date` = CURTIME()';

?>


It goes without saying that there MUST be a record that matches the time EXACTLY, down to the seconds, for it to return a result. If you want it to match a range of time, then just reply with the details.
  #3  
Old 17-Mar-2004, 10:31
cave monkey cave monkey is offline
New Member
 
Join Date: Mar 2004
Location: Juneau, Alaska
Posts: 7
cave monkey is on a distinguished road

Thanks!


Thanks for your response. I found a similar solution through trial and error before reading your post. My final query used TIME_FORMAT() as you recommend....
PHP Code:

$query = "SELECT TIME_FORMAT(`time`,'%h:%i %p')AS `time`,`tide`,`type`,`units` FROM `main` WHERE `date` = NOW()  LIMIT 0, 30";        
$result = mysql_query($query); 


A couple of questions for you, if you have the time. Can DATE_FORMAT() be used when inputting data through a web interface, so that an entry of 03/14/04 is accepted by MySQL as 2004-03-14? Also, is it okay to use multiple AS statements in a query such as mine? Thanks for your time. DM
  #4  
Old 18-Mar-2004, 08:36
JdS's Avatar
JdS JdS is offline
Senior Member
 
Join Date: Aug 2001
Location: KUL, Malaysia
Posts: 3,371
JdS will become famous soon enough
Yes, it's possible but I don't think that's such a good idea.

It's also normal to use multiple AS (or aliases) in your queries; although I don't see it in your example above.
  #5  
Old 18-Mar-2004, 08:41
cave monkey cave monkey is offline
New Member
 
Join Date: Mar 2004
Location: Juneau, Alaska
Posts: 7
cave monkey is on a distinguished road
Thanks again for the help. It's appreciated.
 
 

Recent GIDBlogLast Week of IA Training by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
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

vB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
problem with php5 cgi installation fab13 Apache Web Server Forum 3 19-Nov-2003 09:11
Time Problem in Program MrSmiley C Programming Language 1 03-Nov-2003 12:00
unwanted scrollbar problem kelly001 Web Design Forum 3 24-Oct-2003 10:44

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 15:03.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.