bschultz Posted January 24, 2008 Share Posted January 24, 2008 I'm trying to insert time into a database. The problem that I'm running into is that I need to subtract the time inserted by 20 minutes first. So, 9:15 (nine minutes, 15 seconds) would have to read 11:45 (20 minutes minus 9:15). I can get the time conversion done with this: $myminutes = $_POST[minutes]; $myseconds = $_POST[seconds]; $convertedmins = $myminutes * 60; $totaltime = (1200 - ($convertedmins + $myseconds)); if ($totaltime > 0) { $mins = floor ($totaltime / 60); $secs = $totaltime % 60; printf ("%d:%02.0f", $mins, $secs); } but how do I insert the printf into the database? I've tried making the printf statement a variable, and inserting the variable, but I got a syntax error. Thanks. Link to comment https://forums.phpfreaks.com/topic/87568-convert-before-insert-into-db/ Share on other sites More sharing options...
fenway Posted January 24, 2008 Share Posted January 24, 2008 <SHUDDER> Use "- INTERVAL 20 MINUTE"... don't use PHP for date math. </SHUDDER> Link to comment https://forums.phpfreaks.com/topic/87568-convert-before-insert-into-db/#findComment-448002 Share on other sites More sharing options...
bschultz Posted January 24, 2008 Author Share Posted January 24, 2008 I wanted the information to be inserted as text, and not in TIME format. That's why I converted to seconds, and then did my math. I also didn't know how to make 20:00 be minutes and seconds, and not hours and minutes...that's another reason why I did it the way I did. Like I said before, the value that is "printf 'd" is correct...but I can't get it into the database that way. What's a better way? Link to comment https://forums.phpfreaks.com/topic/87568-convert-before-insert-into-db/#findComment-448044 Share on other sites More sharing options...
fenway Posted January 24, 2008 Share Posted January 24, 2008 Text? Noooo.... How about 00:20:00? Link to comment https://forums.phpfreaks.com/topic/87568-convert-before-insert-into-db/#findComment-448360 Share on other sites More sharing options...
bschultz Posted January 25, 2008 Author Share Posted January 25, 2008 Still doesn't work...unexpected T_VARIABLE on this line $hockeytime = "INTERVAL 20 MINUTE - $_POST[time]"; Here's the whole code: <form name="form1" method="post" action="timecheck.php"> <label> <input type="text" name="time" id="time"> </label> enter the time on the scoreboard here </form> <?php time_format(time, '%i-%s') $hockeytime = "INTERVAL 20 MINUTE - $_POST[time]"; echo $hockeytime; ?> Link to comment https://forums.phpfreaks.com/topic/87568-convert-before-insert-into-db/#findComment-449228 Share on other sites More sharing options...
fenway Posted January 26, 2008 Share Posted January 26, 2008 Format the time correct, then use str_to_date() to make it a true mysql TIME field, and only then case you do date math on it. Link to comment https://forums.phpfreaks.com/topic/87568-convert-before-insert-into-db/#findComment-450119 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.