Skip to content

Instantly share code, notes, and snippets.

@adeel-raza
Created May 13, 2017 08:05
Show Gist options
  • Save adeel-raza/a065a9079c3d51191af73fdda34ee3b1 to your computer and use it in GitHub Desktop.
Save adeel-raza/a065a9079c3d51191af73fdda34ee3b1 to your computer and use it in GitHub Desktop.
Compare 2 dates and find the difference in days between them
<?php
$match_date = \DateTime::createFromFormat("Y-m-d H:i:s", $date_1);
$match_date->setTime( 0, 0, 0 ); // reset time part, to prevent partial comparison
$today = new \DateTime(); // This object represents current date/time
$today->setTime( 0, 0, 0 ); // reset time part, to prevent partial comparison
$diff = $today->diff( $match_date );
$diffDays = (integer)$diff->format( "%R%a" ); // Extract days count in interval
if($diffDays == 0) {
// Current Date is same as $date_1
} else if($diffDays <=30) {
// Current Date is less than or equal to 30 days as $date_1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment