Petty Posted February 23, 2010 Share Posted February 23, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/193061-time-difference/ Share on other sites More sharing options...
Petty Posted February 23, 2010 Author Share Posted February 23, 2010 <?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? Quote Link to comment https://forums.phpfreaks.com/topic/193061-time-difference/#findComment-1016801 Share on other sites More sharing options...
otuatail Posted February 23, 2010 Share Posted February 23, 2010 you could do it in maths x = ((18 * 60) +12) - ((7 * 60) + 12) Then convert back to hours and minutes. Quote Link to comment https://forums.phpfreaks.com/topic/193061-time-difference/#findComment-1016803 Share on other sites More sharing options...
mikesta707 Posted February 23, 2010 Share Posted February 23, 2010 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 Quote Link to comment https://forums.phpfreaks.com/topic/193061-time-difference/#findComment-1016816 Share on other sites More sharing options...
Petty Posted February 23, 2010 Author Share Posted February 23, 2010 First of all Thanks a lot. I want to solve this problem with time functions.I have to convert this unix time to normal time. Maybe i have to subtract 1 hour in the end. Quote Link to comment https://forums.phpfreaks.com/topic/193061-time-difference/#findComment-1016848 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.