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,521 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,809 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: 669 | Replies: 4
Reply
Join Date: May 2005
Posts: 229
Reputation: nathanpacker is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 0
nathanpacker's Avatar
nathanpacker nathanpacker is offline Offline
Posting Whiz in Training

Entering time in form MM:SS

  #1  
Oct 4th, 2007
Hey, this is kind of a dumb problem, but I'm stumped by it. I wrote a little script to track my morning bike rides, and I have a time field to put in the time that I rode. Mind you, this is not the time of the day that I rode, but rather the number of minutes and seconds that my ride lasted.

But with my MySql back end, the only form I could think of was the "time" format, which forces it to HH:MM:SS. My rides rarely last longer than 45 minutes, so I just want to be able to enter the time in the field as MM:SS. Currently I'm just putting it in as "varchar". But eventually I would like to take the information in here and use it to make some graphs and I figure that would be easier if it was a in a time format.

Any idea how?

And lets say that some time in the future, I do ride for over an hour. Is there a way to make it so that I can enter it as MM:SS if it's under an hour, but have the option of entering HH:MM:SS if my ride is longer than an hour?
Last edited by nathanpacker : Oct 4th, 2007 at 12:06 pm.
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Apr 2005
Location: New York state
Posts: 487
Reputation: ShawnCplus will become famous soon enough ShawnCplus will become famous soon enough 
Rep Power: 5
Solved Threads: 73
Sponsor
ShawnCplus's Avatar
ShawnCplus ShawnCplus is offline Offline
Code Monkey

Re: Entering time in form MM:SS

  #2  
Oct 4th, 2007
When you output the time use date_parse which breaks the time up into an associative array so you can display it in any way you would like but it is still stored the same in the database.
Quick note: even if you only enter MM:SS the database will add 00 to the hour so 45 minutes 5 seconds would become 00:45:05
Example:
  1. $timeQuery = "SELECT time FROM rides";
  2. $timeResult = mysql_fetch_row($timeQuery);
  3. $parsedTime = date_parse($timeResult['time']);
  4.  
  5. echo $parsedDate['hour']." hour(s), ".$parsedDate['minute']." minute(s), and ".$parsedDate['second']." second(s).";
Last edited by ShawnCplus : Oct 4th, 2007 at 5:48 pm.
GCS d- s+ a-->? C++(++++) UL+++ P+>+++ L+++ E--- W+++
N+ o K w++(---) O? !M- V PS+>++ PE+ Y+ PGP !t- 5? X- R tv+
b+>++ DI+ D G++>+++ e+ h-->++ r z+*
Reply With Quote  
Join Date: May 2005
Posts: 229
Reputation: nathanpacker is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 0
nathanpacker's Avatar
nathanpacker nathanpacker is offline Offline
Posting Whiz in Training

Re: Entering time in form MM:SS

  #3  
Oct 4th, 2007
Thanks for the reply. That works for the output, however, I tested inputting like you said, and if you simply input 45:05, it will go in the database as 45:05:00, or 45 hours, and 5 minutes. That's my problem.
Reply With Quote  
Join Date: Apr 2005
Location: New York state
Posts: 487
Reputation: ShawnCplus will become famous soon enough ShawnCplus will become famous soon enough 
Rep Power: 5
Solved Threads: 73
Sponsor
ShawnCplus's Avatar
ShawnCplus ShawnCplus is offline Offline
Code Monkey

Re: Entering time in form MM:SS

  #4  
Oct 4th, 2007
Another way to do it would have three separate text boxes for hours, minutes and seconds respectively then when you actually submit it you can concatenate the three and make any empty boxes 00.
GCS d- s+ a-->? C++(++++) UL+++ P+>+++ L+++ E--- W+++
N+ o K w++(---) O? !M- V PS+>++ PE+ Y+ PGP !t- 5? X- R tv+
b+>++ DI+ D G++>+++ e+ h-->++ r z+*
Reply With Quote  
Join Date: Jul 2007
Posts: 3
Reputation: seagram is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
seagram seagram is offline Offline
Newbie Poster

Re: Entering time in form MM:SS

  #5  
Oct 4th, 2007
Though you say you never ride more than one hour, you should handle the case anyway.
First fill the minutes with your ride's duration, possibly overriding 60. Then split this value into hour(s) and remaining minutes. Then concatenate with seconds, and insert the result into database.
  1. $ride_minutes_total = <some integer value>;
  2. $ride_hours = (int)($ride_minutes_total/60);
  3. $ride_minutes = ($ride_minutes_total % 60); // Using modulo operator to get remaining minutes
  4. $ride_seconds = <some integer value>;
  5. $ride_id = <some id>;
  6. $ride_duration = sprintf('%02d:%02d:%02d', $ride_hour, $ride_minutes, $ride_seconds);
  7.  
  8. $timeInsertQuery = "insert INTO rides SET id ='$ride_id', duration='$ride_duration' ";
  9. $result = mysql_query($timeInsertQuery) or die("Query failed");
(Note that IMHO, it is not a good idea to name a database column as 'time' since it is a column type keyword - though MySQL manual says it is allowed as unquoted identifiers for compatibility reasons)
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:12 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC