Jump to content

Time difference


Petty

Recommended Posts

Hi

How could we calculate two diffrent times in command lines?

For example i would like to calculate first time minus (-) secend time and result.

Could someone explain or just verify this small code.

c:\php calculat_time.php 18:12 07:12
First time :18:12 Uhr
Second time: 07:12 Uhr
Result: 11:00 Uhr.

Thanks.

 

Link to comment
https://forums.phpfreaks.com/topic/193061-time-difference/
Share on other sites

<?php
$first_time=$argv[1];
$second_time=$argv[2];
echo "First time :", $first_time ,"\n","Second time:",$second_time, "\n";


$first_time=strtotime($first_time);
$second_time=strtotime($second_time);

echo "First time stamp:", $first_time ,"\n","Second time stamp:",$second_time, "\n";
$res_time=$first_time-$second_time-strftime('-1 hour');

echo date('H:i', $res_time);

?>

Im doing something wrong but i couldnt figure out?

Link to comment
https://forums.phpfreaks.com/topic/193061-time-difference/#findComment-1016801
Share on other sites

The following worked for me.

$first_time="18:12";
$second_time="07:12";
echo "First time :", $first_time ,"<br />","Second time:",$second_time, "<br />";


$first_time=strtotime($first_time);
$second_time=strtotime($second_time);

echo "First time stamp:", $first_time ,"<br />","Second time stamp:",$second_time, "<br />";
$res_time=$first_time-$second_time;

$r = $res_time / 3600;//num hours
$min = $res_time%3600;

echo "$r hours and $min minutes";

you could change to echo to be

echo "$r:$min";

 

if you want to make sure they are padded with zeros (IE 07 insead of 7,) you can do

$r = str_pad($r, 2, "0", STR_PAD_LEFT);
$min = str_pad($min, 2, "0", STR_PAD_LEFT);

 

to pad them

 

Link to comment
https://forums.phpfreaks.com/topic/193061-time-difference/#findComment-1016816
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.