[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 206
  • Last Modified:

Is there a way of manipulating user entered times?

I am working on a project where a user enters a start time and a finish time. I want to be able to tell how long was taken. As we are working with time I cant think of a simple way of doing this, does anyone know of any built-in functions I could use in php?
The time will be in the format
MM:SS:tenths of second(I think its tenths!).
Thanks in advance.
0
jdav3579
Asked:
jdav3579
  • 3
2 Solutions
 
alextr2003frCommented:
try something like :
<?php
  $t_start = "17:00:00";
  $t_end = "18:00:00";
  echo date("H:i:s",strtotime($t_end) - strtotime($t_start));
?>
0
 
jdav3579Author Commented:
Nope, no joy. it only displayed $t_end value.
I am afraid I am quite new to php and programming.
0
 
alextr2003frCommented:
try something like this :
<?php
//format MM:SS:tenths of second
$start = "01:00:00";
$end = "02:12:01";
function convert_ts($ttime) {
        preg_match_all("/\d*/",$ttime,$t1);
        $t = ($t1[0][0])*600+($t1[0][2])*10+($t1[0][4]);
        return $t;
}
function time_diff_ms($start,$end) {
        $t = convert_ts($end) - convert_ts($start);
        return $t; //ts
}
echo 'Difference = '.(time_diff_ms($start,$end)/10).' seconds';
?>
this is the long version, a short version can be done in one function but maybe you will need to conver it to the tenths of seconds elsewhere. Hope this helps.
0
 
alextr2003frCommented:
same but shorter :
<?
//format MM:SS:tenths of second
$start = "01:00:00";
$end = "02:12:01";
function time_diff_mss($start,$end) {
        preg_match_all("/\d*/",$start,$t1);
        preg_match_all("/\d*/",$end,$t2);
        $t = ($t2[0][0])*600+($t2[0][2])*10+($t2[0][4]) - ($t1[0][0])*600+($t1[0][2])*10+($t1[0][4]);
        return $t;
}
echo 'Difference = '.(time_diff_ms($start,$end)/10).' seconds';
?>
0
 
Marvin_CarredoCommented:
<?
function GetTime ($timedifference) {

   if ($timedifference >= 3600) {
       $hval = ($timedifference / 3600);
       $hourtime = intval($hval);

       $leftoverhours = ($timedifference % 3600);

       $mval = ($leftoverhours / 60);
       $minutetime = intval($mval);

       $leftoverminutes = ($leftoverhours % 60);
       $secondtime = intval($leftoverminutes);

       $hourtime = str_pad($hourtime, 2, "0", STR_PAD_LEFT);
       $minutetime = str_pad($minutetime, 2, "0", STR_PAD_LEFT);
       $secondtime = str_pad($secondtime, 2, "0", STR_PAD_LEFT);

       return "$hourtime:$minutetime:$secondtime";
   }

   if ($timedifference >= 60) {

       $hourtime = 0;

       $mval = ($timedifference / 60);
       $minutetime = intval($mval);

       $leftoverminutes = ($timedifference % 60);
       $secondtime = intval($leftoverminutes);

       $hourtime = str_pad($hourtime, 2, "0", STR_PAD_LEFT);
       $minutetime = str_pad($minutetime, 2, "0", STR_PAD_LEFT);
       $secondtime = str_pad($secondtime, 2, "0", STR_PAD_LEFT);

       return "$hourtime:$minutetime:$secondtime";
   }

 
   $hourtime = 0;
   $minutetime = 0;
   if ($timedifference < 0 ) { $secondtime = 0; }
   else {    $secondtime = $timedifference; }

   $hourtime = str_pad($hourtime, 2, "0", STR_PAD_LEFT);
   $minutetime = str_pad($minutetime, 2, "0", STR_PAD_LEFT);
   $secondtime = str_pad($secondtime, 2, "0", STR_PAD_LEFT);

   return "$hourtime:$minutetime:$secondtime";
 
}

$mytime = GetTime(1101960767 - 1101961767);
echo $mytime;

?>
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now