Created
February 7, 2019 10:16
-
-
Save relliv/993643a958f5803f769b1b4cb2588b2f to your computer and use it in GitHub Desktop.
PHP Date Comparer
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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