Skip to content

Instantly share code, notes, and snippets.

@relliv
Created February 7, 2019 10:16
Show Gist options
  • Save relliv/993643a958f5803f769b1b4cb2588b2f to your computer and use it in GitHub Desktop.
Save relliv/993643a958f5803f769b1b4cb2588b2f to your computer and use it in GitHub Desktop.
PHP Date Comparer
/**
* Date comparer
*
* @param string $date: current date
* @param string $date2: target date
* @param bool $diff: date diff or past time condition
* @return bool|array
*/
public function dateCompare($current, $expire, $diff = false)
{
$expire_date = new DateTime($expire);
$current_date = new DateTime($current);
$interval = $expire_date->diff($current_date);
// pure date diff result
if ($diff){
return $interval;
}
// for past time conditions
foreach ($interval as $key => $value) {
if ($key == 'invert') {
return $value < 1 ? false : true;
}
}
return false;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment