Skip to content

Instantly share code, notes, and snippets.

@phpfiddle
Created August 1, 2015 07:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save phpfiddle/bb35c766e462dfeff79b to your computer and use it in GitHub Desktop.
Save phpfiddle/bb35c766e462dfeff79b to your computer and use it in GitHub Desktop.
[ Posted by Non_zero ] This is the code for getting date differences in php.
<?php
echo 'For Yearly ::<br/>';
$cur_datetime = new DateTime();
echo '<br/>'.date('Y-m-d');
echo '<br/>';
$app_datetime = new DateTime("2014-08-02");
echo date('2014-08-02');
echo '<br/>';
$difference = $cur_datetime->diff($app_datetime);
echo '<br/>';
//yr diff:
if($difference->y>=1 && $difference->m>=0 && $difference->d>=0){
echo 'Yr completed !!!<br/>';
}
else{
echo 'Yr not completed';
echo 'Difference: '.$difference->y.' years, '
.$difference->m.' months, '
.$difference->d.' days';
echo '<br/>';
}
echo '<br/><br/><br/>';
echo 'For Monthly::';
$datetime_cur = new DateTime();
echo '<br/>'.date('Y-m-d');
echo '<br/>';
$datetime_app = new DateTime('2014-05-01');
echo '<br/>'.date('2014-05-01');
echo '<br/>';
$date_diff = $datetime_cur->diff($datetime_app);
if($date_diff->y >=0 && $date_diff->m >=1 && $date_diff->d ==0){
echo 'Month completed.';
echo 'Difference: '.$date_diff->y.' years, '
.$date_diff->m.' months, '
.$date_diff->d.' days';
echo '<br/><br/><br/>';
}
else{
echo 'Month not completed.';
echo 'Difference: '.$date_diff->y.' years, '
.$date_diff->m.' months, '
.$date_diff->d.' days';
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment