Home » PHP

PHP code to get time difference

In this code example we are taking two times and getting their differences, following code can be used to get exact time difference between two times.

Source Code and Output to get time difference

<?php
	$time1="09:00";
	$time2="17:00";
	function get_time_difference($time1, $time2)
	{
		$time1 = strtotime($time1);
		$time2 = strtotime($time2);
		
		if ($time2 < $time1) 
		{
			$time2 += 86400;
		}
		
		return date("H:i", strtotime("00:00") + ($time2 - $time1));
	}

	$timediff= get_time_difference($time1, $time2); // 12:15:29
	printf("Difference between in time : " .$timediff);
	print "</br>";
?>

Output

Difference between in time : 08:00




Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.



Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing » Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.