Jump to content

time comparison help


EchoFool

Recommended Posts

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?

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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 ...."

 

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.