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

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
djdon11
Forum Commoner
Posts: 90
Joined: Wed Jun 20, 2007 5:03 pm

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

Post 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 ????
User avatar
aceconcepts
DevNet Resident
Posts: 1424
Joined: Mon Feb 06, 2006 11:26 am
Location: London

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

Post by aceconcepts »

Hi,

Take a look at: http://uk3.php.net/strtotime
djdon11
Forum Commoner
Posts: 90
Joined: Wed Jun 20, 2007 5:03 pm

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

Post 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);
Festy
Forum Commoner
Posts: 28
Joined: Wed Jan 30, 2008 10:01 pm

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

Post 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>";
 
?>
 
djdon11
Forum Commoner
Posts: 90
Joined: Wed Jun 20, 2007 5:03 pm

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

Post 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
Festy
Forum Commoner
Posts: 28
Joined: Wed Jan 30, 2008 10:01 pm

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

Post 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().
User avatar
JamesRavenscroft
Forum Newbie
Posts: 10
Joined: Thu Jan 31, 2008 3:45 am
Location: West Midlands, United Kingdom

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

Post 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.
 
 
Post Reply