Page 1 of 1

how to get minutes from time difference coming in time()

Posted: Thu Jan 31, 2008 2:10 am
by djdon11
Hi friends

i have a starttime in the format of $starttime = time() and endtime as $endtime = time()

now i am trying to come out with the time difference $endtime-$starttime in the minutes

how can i get the minutes from this output ????

Re: how to get minutes from time difference coming in time()

Posted: Thu Jan 31, 2008 3:06 am
by aceconcepts
Hi,

Take a look at: http://uk3.php.net/strtotime

Re: how to get minutes from time difference coming in time()

Posted: Thu Jan 31, 2008 3:39 am
by djdon11
I am doing this , tell me whether it is right or wrong?


$starttime = time(); this is when my quiz is start

$endtime = time(); this is when my quiz is end

$total_time_taken = $endtime-$starttime;
$real = date("i",$total_time_taken);

Re: how to get minutes from time difference coming in time()

Posted: Thu Jan 31, 2008 3:46 am
by Festy
You have to convert the times to unix timestamp before performing the '-' operation.

Try this code below -

Code: Select all

 
<?php
 
$nowdate = strtotime("8 April 2007");
$thendate = strtotime("2 May 2006");
 
$datediff = ($nowdate - $thendate);
 
echo $datediff." Seconds<br>";
echo round($datediff / 60)." Minutes<br>";
echo round($datediff / 3600)." Hours<br>";
echo round($datediff / 86400)." Days<br>";
 
?>
 

Re: how to get minutes from time difference coming in time()

Posted: Thu Jan 31, 2008 3:52 am
by djdon11
>You have to convert the times to unix timestamp before performing the '-' operation.

but i think the function time() also gives current time in the unix time stamp

is it true ?
http://in2.php.net/time

Re: how to get minutes from time difference coming in time()

Posted: Thu Jan 31, 2008 4:02 am
by Festy
djdon11 wrote:>You have to convert the times to unix timestamp before performing the '-' operation.

but i think the function time() also gives current time in the unix time stamp

is it true ?
http://in2.php.net/time
Yes it's true. What I mean is - to find the difference using strtotime() function, you have to pass an english date as an argument in strtotime().

Re: how to get minutes from time difference coming in time()

Posted: Thu Jan 31, 2008 4:06 am
by JamesRavenscroft
I was pretty sure that was the case. The approach you are taking is exactly how I would do it.

Maybe do some debugging. Print the start time, end time and the difference time and see if you get output for all 3. Maybe one of the variables is being set funny. Although, I can't see why that would be the case.

Another alternative might be to try manually setting the number to minutes like so:

Code: Select all

 
$starttime = time(); this is when my quiz is start
 
$endtime = time(); this is when my quiz is end
 
$total_time_taken = $endtime-$starttime;
 
$minutes = $total_time_taken / 60; //divide by 60 to get minutes from seconds.