Saturday, March 5, 2011

Date diff PHP code

$datetime1 = new DateTime('2009-10-11');
$datetime2 = new DateTime('2009-10-13');
$interval = $datetime1->diff($datetime2);
echo $interval->format('%R%a days');

$datetime1 = date_create('2009-10-11');
$datetime2 = date_create('2009-10-13');
$interval = date_diff($datetime1, $datetime2);
echo $interval->format('%R%a days');
function DateDiff($strDate1,$strDate2)
{
return (strtotime($strDate2) - strtotime($strDate1))/ ( 60 * 60 * 24 ); // 1 day = 60*60*24
}
function TimeDiff($strTime1,$strTime2)
{
return (strtotime($strTime2) - strtotime($strTime1))/ ( 60 * 60 ); // 1 Hour = 60*60
}
function DateTimeDiff($strDateTime1,$strDateTime2)
{
return (strtotime($strDateTime2) - strtotime($strDateTime1))/ ( 60 * 60 ); // 1 Hour = 60*60
}

echo "Date Diff = ".DateDiff("2008-08-01","2008-08-31")."<br>";
echo "Time Diff = ".TimeDiff("00:00","19:00")."<br>";
echo "Date Time Diff = ".DateTimeDiff("2008-08-01 00:00","2008-08-01 19:00")."<br>";
Related

Refer

  • http://www.blogger.com/post-create.g?blogID=34115975
  • http://stackoverflow.com/questions/676824/how-to-calculate-the-difference-between-two-dates-using-php
  • http://www.phpf1.com/tutorial/php-date-difference.html
  • http://www.thaicreate.com/php/php-date-diff.html
  • http://www.plus2net.com/php_tutorial/date-diff.php
  • http://www.phpzealots.com/date_diff

No comments:

Post a Comment