0

How can I calculate the total travel time in PHP:

Per mile Duration: 00:11:40

Total miles: 177

Total duration: 34:25:00

I've tried different ways, but can not get it done.

$distance = "177";
$parsetime = strtotime("00:11:40");

$otfrom_string  = time();
$needed = $parstime*$distance;
$otto_string = $otfrom_string+$needed;

echo date("H:i:s",$otfrom_string)."<br />";
echo date("H:i:s",$otto_string)."<br />";

$start = $otfrom_string;
$end = $otto_string;
$elapsed = $end - $start;
echo date("H:i:s", $elapsed);
WillieBoy
  • 5
  • 7
  • 1
    possible duplicate of [PHP How to find the time elapsed since a date time?](http://stackoverflow.com/questions/2915864/php-how-to-find-the-time-elapsed-since-a-date-time) – John Conde May 06 '13 at 13:10
  • what output are you getting? or, if you're not getting any output, what error are you getting? why do you think this is the case? – user428517 May 06 '13 at 13:10
  • This is the output: 2013-05-06 14:37:17 1927-05-06 20:30:37 15:02:04 – WillieBoy May 06 '13 at 13:14
  • Isn't $elapsed equal to $needed? – RST May 06 '13 at 13:21

2 Answers2

0

strtotime Returns a timestamp on success, false otherwise. i THINK you are multipling a timestamp for 177 which is not the way to calculate the duration.

I suggest you to convert your string in seconds, then multiply for 177 then ADD result of below code to the current time and you get the "arrival" time.

To convert your string to seconds use

    $timestr = '00:30:00';   
    $parts = explode(':', $timestr);
    $seconds = ($parts[0] * 60 * 60) + ($parts[1] * 60) + $parts[2];
Madthew
  • 686
  • 6
  • 15
0
$time = '00:11:40';
$distance = 177;

list($h,$m,$s) = explode(':',$time);

$nbSec = $h * 3600 + $m * 60 + $s;
$totalDuration = $nbSec * $distance;

echo nbSecToString($totalDuration);//print 34:00:25


function nbSecToString($nbSec) {

    $tmp = $nbSec % 3600;
    $h = ($nbSec - $tmp ) / 3600;
    $s = $tmp % 60;
    $m = ( $tmp - $s ) / 60;

    $h = str_pad($h, 2, "0", STR_PAD_LEFT);
    $m = str_pad($m, 2, "0", STR_PAD_LEFT);
    $s = str_pad($s, 2, "0", STR_PAD_LEFT);

    return "$h:$m:$s";
}

Demo