Jump to content

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.