Re: [PHP] Difference between 2 time entries

2006-07-20 Thread tedd
At 12:41 PM +0200 7/20/06, Chris Grigor wrote:
>Morning all,
>
>I am looking to get the differnce in hours / minutes between 2 values.
>
>Currently I have 2 time entries being retruned from mysql, one which is a
>start time and
>the other which is a finish time.
>
>So
>
>$start = '13:12:17';
>$finish = '23:12:17';
>
>How would one get the differnce between these 2 times??
>
>I have looked at using the following but am not to sure
>
>function timeDiff($firstTime,$lastTime)
>{
>
>// convert to unix timestamps
>$firstTime=strtotime($firstTime);
>$lastTime=strtotime($lastTime);
>
>// perform subtraction to get the difference (in seconds) between times
>$timeDiff=$lastTime-$firstTime;
>
>// return the difference
>return $timeDiff;
>}
>
>//Usage :
>echo timeDiff("$start","$finish");

Chris:

You're about there, you have the seconds, just add this:

$hours = floor($timeDiff/3600);
$timeDiff = $timeDiff - ($hours * 3600);
$minutes = floor($timeDiff/60);
$timeDiff = $timeDiff - ($minutes * 60);
$seconds = $timeDiff;
echo($hours);
echo("");  
echo($minutes);
echo("");  
echo($seconds);

I'm surprised that php doesn't have a function to do this -- but maybe I didn't 
RTFM enough.

In any event, hth's

tedd
-- 

http://sperling.com  http://ancientstones.com  http://earthstones.com

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] Difference between 2 time entries

2006-07-20 Thread tedd
At 1:51 PM +0200 7/20/06, nicolas figaro wrote:
>IMHO, the best is to generate a timestamp using mktime for each date.
>you can the calculate the difference of timestamps and convert it back using 
>date.
>
>$tm_start = mktime(substr($start,0,2),substr($start,3,2), substr($start,5,2));
>$tm_finish = mktime(substr($finish,0,2),substr($finish,3,2), 
>substr($finish,5,2));
>
>$tm_diff = $tm_finish -tm_start;
>
>print date("H:i",$tm_diff);
>
>I don't know if date accepts negative timestamps, so be sure $finish is later 
>than $start
>(you can also put the day in case $finish = "00:00:12" and $start  
>="15:00:00").
>
>hope this'll help
>
>N F


NF:

Not quite.

Your:

   substr($start,5,2)

should be:

   substr($start,6,2)

Your:

   $tm_diff = $tm_finish -tm_start;

Should be:

   $tm_diff = $tm_finish - $tm_start;

And lastly,

   print date("H:i",$tm_diff);

Doesn't print the hours and seconds that have elapsed.

I thought that php had a function where you could send it a number of seconds 
and it would return the number of years, months, days, hours and seconds, but I 
couldn't find any. But, date() doesn't.

tedd
-- 

http://sperling.com  http://ancientstones.com  http://earthstones.com

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] Difference between 2 time entries

2006-07-20 Thread John Meyer

|Found this on the PHP web site,
you can add the conversions as the first lines in the functions.

function callDuration($dateTimeBegin,$dateTimeEnd) {
 
$dif=$dateTimeEnd - $dateTimeBegin;


$hours = floor($dif / 3600);
$temp_remainder = $dif - ($hours * 3600);
 
$minutes = floor($temp_remainder / 60);

$temp_remainder = $temp_remainder - ($minutes * 60);
 
$seconds = $temp_remainder;
   
// leading zero's - not bothered about hours

$min_lead=':';
if($minutes <=9)
  $min_lead .= '0';
$sec_lead=':';
if($seconds <=9)
  $sec_lead .= '0';
 
 // difference/duration returned as Hours:Mins:Secs e.g. 01:29:32


 return $hours.$min_lead.$minutes.$sec_lead.$seconds;
 
  }

|
Chris Grigor wrote:

Morning all,


I am looking to get the differnce in hours / minutes between 2 values.

Currently I have 2 time entries being retruned from mysql, one which is a
start time and
the other which is a finish time.

So

$start = '13:12:17';
$finish = '23:12:17';

How would one get the differnce between these 2 times??

I have looked at using the following but am not to sure



function timeDiff($firstTime,$lastTime)
{

// convert to unix timestamps
$firstTime=strtotime($firstTime);
$lastTime=strtotime($lastTime);

// perform subtraction to get the difference (in seconds) between times
$timeDiff=$lastTime-$firstTime;

// return the difference
return $timeDiff;
}

//Usage :
echo timeDiff("$start","$finish");

Thanks

Chris

  


--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



[PHP] Difference between 2 time entries

2006-07-20 Thread Chris Grigor
Morning all,


I am looking to get the differnce in hours / minutes between 2 values.

Currently I have 2 time entries being retruned from mysql, one which is a
start time and
the other which is a finish time.

So

$start = '13:12:17';
$finish = '23:12:17';

How would one get the differnce between these 2 times??

I have looked at using the following but am not to sure



function timeDiff($firstTime,$lastTime)
{

// convert to unix timestamps
$firstTime=strtotime($firstTime);
$lastTime=strtotime($lastTime);

// perform subtraction to get the difference (in seconds) between times
$timeDiff=$lastTime-$firstTime;

// return the difference
return $timeDiff;
}

//Usage :
echo timeDiff("$start","$finish");

Thanks

Chris

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] Difference between 2 time entries

2006-07-20 Thread nicolas figaro

Chris Grigor a écrit :

Morning all,


I am looking to get the differnce in hours / minutes between 2 values.

Currently I have 2 time entries being retruned from mysql, one which is a
start time and
the other which is a finish time.

So

$start = '13:12:17';
$finish = '23:12:17';

How would one get the differnce between these 2 times??
  

IMHO, the best is to generate a timestamp using mktime for each date.
you can the calculate the difference of timestamps and convert it back 
using date.


$tm_start = mktime(substr($start,0,2),substr($start,3,2), 
substr($start,5,2));
$tm_finish = mktime(substr($finish,0,2),substr($finish,3,2), 
substr($finish,5,2));


$tm_diff = $tm_finish -tm_start;

print date("H:i",$tm_diff);

I don't know if date accepts negative timestamps, so be sure $finish is 
later than $start
(you can also put the day in case $finish = "00:00:12" and $start  
="15:00:00").


hope this'll help

N F

I have looked at using the following but am not to sure



function timeDiff($firstTime,$lastTime)
{

// convert to unix timestamps
$firstTime=strtotime($firstTime);
$lastTime=strtotime($lastTime);

// perform subtraction to get the difference (in seconds) between times
$timeDiff=$lastTime-$firstTime;

// return the difference
return $timeDiff;
}

//Usage :
echo timeDiff("$start","$finish");

Thanks

Chris

  




Nicolas Figaro  <[EMAIL PROTECTED] >
SDV plurimédia


-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

[PHP] Difference between 2 time entries

2006-07-20 Thread Chris Grigor
Morning all,


I am looking to get the differnce in hours / minutes between 2 values.

Currently I have 2 time entries being retruned from mysql, one which is a
start time and
the other which is a finish time.

So

$start = '13:12:17';
$finish = '23:12:17';

How would one get the differnce between these 2 times??

I have looked at using the following but am not to sure



function timeDiff($firstTime,$lastTime)
{

// convert to unix timestamps
$firstTime=strtotime($firstTime);
$lastTime=strtotime($lastTime);

// perform subtraction to get the difference (in seconds) between times
$timeDiff=$lastTime-$firstTime;

// return the difference
return $timeDiff;
}

//Usage :
echo timeDiff("$start","$finish");

Thanks

Chris

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php