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!.

Recommended Answers

All 4 Replies

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"; ?>");

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'));
?>

Is thread solved?

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/>";
?>
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.