Page 1 of 1

elapsed time

Posted: Sun Jan 23, 2005 6:00 pm
by froth
I have 10 variables:

The minute of time #1
The hour of time #1
The day of time #1
The month of time #1
The year of time #1

The minute of time #2
The hour of time #2
The day of time #2
The month of time #2
The year of time #2

And I need to find the exact time in minutes between those two times. I don't need anything but the minutes, but the other numbers must be taken into consideration (like if one time is 23:55 and the other is 0:15, day has to be considered). Does anybody have a prewritten function that can do this? I've been working on this for awhile and don't want to have to write it again.

Thanks much in advance :)

Posted: Sun Jan 23, 2005 6:26 pm
by The Monkey
I highly recommend using Unix Timestamps via the PHP time() and date functions.

Posted: Sun Jan 23, 2005 7:13 pm
by timvw
last time i demonstrate the power of http://www.php.net/strtotime on this forum :)

Code: Select all

$t1 = strtotime("$year1-$month1-$day1 $hour1:$minute1:$second1");
$t2 = strtotime("$year2-$month2-$day2 $hour2:$minute2:$second2");
$diff = $t1 - $t2;

*feels ignorant*

Posted: Sun Jan 23, 2005 7:18 pm
by froth
So how do you extract the # of minutes out of diff? :oops:

Posted: Sun Jan 23, 2005 7:34 pm
by timvw
rule of 3 :p

(1) 60 * second = 1 * minute

=>

(2) 1 * seconds = 1 / 60 minute

=>

(3) x * seconds = x / 60 minute

Posted: Sun Jan 23, 2005 7:41 pm
by froth
oh so diff is just the # of seconds. I can do that.

btw congrats on your thousanth post.

Posted: Sun Jan 23, 2005 7:45 pm
by feyd
ooo... I didn't even notice that.. yay tim. ;)

also...

Posted: Sun Jan 23, 2005 7:49 pm
by froth
also, does that code work on a 24-hour clock?

Posted: Sun Jan 23, 2005 8:10 pm
by feyd
it doesn't care about time of day, week, year, century (for the most part)

it's not working...

Posted: Sun Jan 23, 2005 8:54 pm
by froth

Code: Select all

$t1 = strtotime("$begyr-$begmnth-$begdy $beghr:$begminte:0");
$t2 = strtotime("$yr-$mnth-$dy $ahour:$aminute:0");
$diff = ($t1 - $t2) / 60;
diff should be the # of minutes, right?

it doesn't work. for times ~30 minutes apart, diff is 782.

Posted: Sun Jan 23, 2005 10:22 pm
by timvw
echo out the strings you call strtotime with.....

because overhere everythings works as it should...

Code: Select all

$t1 = strtotime('2005-01-24 23:55:00');
$t2 = strtotime('2005-01-25 00:15:00');
$diff = ($t2 - $t1) / 60;
echo $diff;


only 6000 post or more to get at feyd's level :)