1

hi, i need to subtract one date form the other and take the difference in minutes or seconds..
i'm getting the first date from the data base in 'Y-m-d H:i:s' format and second date is the current date (same format). and i want to know the difference between them in minutes and seconds.
thanx!.

4
Contributors
4
Replies
11
Views
5 Years
Discussion Span
Last Post by lyrico
0

sample :

print("<?php $to_time=strtotime("2011-01-13 10:42:00");
$from_time=strtotime("2011-01-12 10:21:00"); 
echo round(abs($to_time - $from_time) / 60,2)." minute"; ?>");
0

I have created this function.
You can use it or customize it as per requirement.

<?
	function dateDifference($date1, $date2)
	{		
		$date1=strtotime($date1);
		$date2=strtotime($date2); 
		$diff = abs($date1 - $date2);
		
		$day = $diff/(60*60*24); // in day
		$dayFix = floor($day);
		$dayPen = $day - $dayFix;
		if($dayPen > 0)
		{
			$hour = $dayPen*(24); // in hour (1 day = 24 hour)
			$hourFix = floor($hour);
			$hourPen = $hour - $hourFix;
			if($hourPen > 0)
			{
				$min = $hourPen*(60); // in hour (1 hour = 60 min)
				$minFix = floor($min);
				$minPen = $min - $minFix;
				if($minPen > 0)
				{
					$sec = $minPen*(60); // in sec (1 min = 60 sec)
					$secFix = floor($sec);
				}
			}
		}
		$str = "";
		if($dayFix > 0)
			$str.= $dayFix." day ";
		if($hourFix > 0)
			$str.= $hourFix." hour ";
		if($minFix > 0)
			$str.= $minFix." min ";
		if($secFix > 0)
			$str.= $secFix." sec ";
		return $str;
	}
	echo '<br>Difference is : '.dateDifference("2011-09-18 10:00:00", date('Y-m-d H:i:s'));
?>
0

I think this can help you.

<?php

$t1 = strtotime('2011/09/19 09:18:22 AM');
$t2 = strtotime('2011/09/22 03:22:18 PM');

$delta_T = ($t2 - $t1);

$day = round(($delta_T % 604800) / 86400); 
$hours = round((($delta_T % 604800) % 86400) / 3600); 
$minutes = round(((($delta_T % 604800) % 86400) % 3600) / 60); 
$sec = round((((($delta_T % 604800) % 86400) % 3600) % 60));



echo $day." days"."<br/>";
echo $hours." hours"."<br/>";
echo $minutes." minutes"."<br/>";
echo $sec." sec"."<br/>";
?>
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.