Skip to content

Instantly share code, notes, and snippets.

@ludwigmair
Created July 8, 2013 07:23
Show Gist options
  • Save ludwigmair/5946834 to your computer and use it in GitHub Desktop.
Save ludwigmair/5946834 to your computer and use it in GitHub Desktop.
<?php
// >= 5.4
$date1 = new DateTime('2013-02-05');
$date2 = new DateTime('2013-03-05');
$interval = $date1->diff($date2);
// Suppose you only want to display the days
printf("difference %d days <br>",$interval->days);
// bis 5.3
$date1 = '2013-02-05';
$date2 = '2013-03-05';
$diff = abs(strtotime($date2) - strtotime($date1));
$years = floor($diff / (365*60*60*24));
$months = floor(($diff - $years * 365*60*60*24) / (30*60*60*24));
$days = floor(($diff - $years * 365*60*60*24 - $months*30*60*60*24)/ (60*60*24));
printf(" %d days<br>", $days);
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment