EchoFool Posted September 1, 2008 Share Posted September 1, 2008 I am having problems with my time comparison im trying to work out how much time is left from when some thing started by checking how many minutes have been from the starting point. The length of time it takes is 90 minutes so i tried this: <?php $Today = date("d-m-y", time()); $Get = mysql_query("SELECT TotalSold,Time FROM tours WHERE Date='$Today' AND Url='$Url'") Or die(mysql_error()); $row = mysql_fetch_assoc($Get); $Time = $row['Time']; // time it started $TimeNow = date("H:i:s",time()); // server time //convert to numbers for maths $Time2 = strtotime($Time); $TimeNow2 = strtotime($TimeNow); $Difference = $Time2 - $TimeNow2; // subtract the two to find out the diff $Difference = $Difference / 60; // divide by 60 to turn in to minutes $Difference = round($Difference); // round to nearest full integer $TimeLeft = 90 - $Difference; //subtract that from 90 minutes which is maximum waiting time But my $TimeLeft doesn't show a correct time left =/ Any one able to see where my logic went wrong? Quote Link to comment Share on other sites More sharing options...
thebadbad Posted September 1, 2008 Share Posted September 1, 2008 $TimeNow2 - $Time2, not the other way around Quote Link to comment Share on other sites More sharing options...
EchoFool Posted September 1, 2008 Author Share Posted September 1, 2008 $TimeNow2 - $Time2, not the other way around Ok tried that and this is what resulted: Started At: 00:06:00 // this was midnight today City Time: 14:24:46 // this is now 2pm today (14 hours later) Time Left: 76 minutes. < < this is the $TimeLeft Quote Link to comment Share on other sites More sharing options...
thebadbad Posted September 1, 2008 Share Posted September 1, 2008 Since you subtract the difference from 90 (minutes), the difference should be less than 90 minutes obviously. Else; are you sure $row['Time'] is in HH:MM:SS format? Another thing is, this won't work when the span is crossing midnight. Much easier to store timestamps in the DB, since they also contain the date. Quote Link to comment Share on other sites More sharing options...
EchoFool Posted September 1, 2008 Author Share Posted September 1, 2008 The $Time variable is coming from the DB ...and yes it is in that format of HH : MM : SS But the $TimeNow is the "now" instance so i would of thought the php function to get the server time would suffice? Quote Link to comment Share on other sites More sharing options...
blinky001 Posted September 1, 2008 Share Posted September 1, 2008 You need to use the full data in your functions - i.e. pull a bit more from your database. E.g. <?php $time = '14:26:13'; $date = '2008-09-01'; $timezone = 'GMT'; $new_time = strtotime($date.' '.$time.' '.$timezone); ?> Or better, you should store date and time together (datetime) and then pull them from the database using "SELECT UNIX_TIMESTAMP(`datetime_field`) WHERE ...." Quote Link to comment Share on other sites More sharing options...
EchoFool Posted September 1, 2008 Author Share Posted September 1, 2008 Ok this is getting a bit far from my skill level. How will that make the calculation work exactly? Quote Link to comment Share on other sites More sharing options...
thebadbad Posted September 1, 2008 Share Posted September 1, 2008 Firstly, your example was bad because of this: Since you subtract the difference from 90 (minutes), the difference should be less than 90 minutes obviously. Secondly, Another thing is, this won't work when the span is crossing midnight. Much easier to store timestamps in the DB, since they also contain the date. An example of the above; a timespan of 40 minutes crossing midnight: $Time is 23:30:00 and $TimeNow is 00:10:00 (the next day compared to $Time). $TimeNow2 - $Time2 would then be negative, because the format doesn't say anything about the date. Edit: That was rubbish.. To solve the problem, you could say (60*60*24) + answer (which is negative) to get the right amount of seconds. Quote Link to comment Share on other sites More sharing options...
The Little Guy Posted September 1, 2008 Share Posted September 1, 2008 Something like this??? $minutes = 90; $sql = mysql_query("SELECT * FROM `tableName` WHERE `date` < DATE_SUB(NOW(),INTERVAL $minutes MINUTE)"); while($row = mysql_fetch_array($sql)){ echo $row['columnName']; } Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.