Skip to content

Instantly share code, notes, and snippets.

@guimadaleno
Last active September 1, 2022 15:28
Show Gist options
  • Save guimadaleno/49f1f47d89c27e7626c9e1dc0b0a035f to your computer and use it in GitHub Desktop.
Save guimadaleno/49f1f47d89c27e7626c9e1dc0b0a035f to your computer and use it in GitHub Desktop.
Small date comparison functions
<?php
/**
* Test if date is equal
* @param string $date1
* @param string $date2 (default today timestamp)
* @param string $format (default d/m/Y)
* @return bool
*/
function is_date_equal ($date1, $date2 = 0, $format = "d/m/Y")
{
$date1 = $dt = DateTime::createFromFormat('d/m/Y', $date1) -> getTimestamp();
$date2 = (isset($date2)) ? $dt = DateTime::createFromFormat($format, $date2) -> getTimestamp() : time();
return ($date1 == $date2) ? true : false;
}
/**
* Compare date greater than
* @param string $date1
* @param string $date2 (default today timestamp)
* @param string $format (default d/m/Y)
* @return bool
*/
function is_date_greater ($date1, $date2 = 0, $format = "d/m/Y")
{
$date1 = $dt = DateTime::createFromFormat('d/m/Y', $date1) -> getTimestamp();
$date2 = (isset($date2)) ? $dt = DateTime::createFromFormat($format, $date2) -> getTimestamp() : time();
return ($date1 > $date2) ? true : false;
}
/**
* Compare date greater or equal than
* @param string $date1
* @param string $date2 (default today timestamp)
* @param string $format (default d/m/Y)
* @return bool
*/
function is_date_greater_or_equal ($date1, $date2 = 0, $format = "d/m/Y")
{
$date1 = $dt = DateTime::createFromFormat('d/m/Y', $date1) -> getTimestamp();
$date2 = (isset($date2)) ? $dt = DateTime::createFromFormat($format, $date2) -> getTimestamp() : time();
return ($date1 >= $date2) ? true : false;
}
/**
* Compare date lower than
* @param string $date1
* @param string $date2 (default today timestamp)
* @param string $format (default d/m/Y)
* @return bool
*/
function is_date_lower ($date1, $date2 = 0, $format = "d/m/Y")
{
$date1 = $dt = DateTime::createFromFormat('d/m/Y', $date1) -> getTimestamp();
$date2 = (isset($date2)) ? $dt = DateTime::createFromFormat($format, $date2) -> getTimestamp() : time();
return ($date1 < $date2) ? true : false;
}
/**
* Compare date lower or equal than
* @param string $date1
* @param string $date2 (default today timestamp)
* @param string $format (default d/m/Y)
* @return bool
*/
function is_date_lower_or_equal ($date1, $date2 = 0, $format = "d/m/Y")
{
$date1 = $dt = DateTime::createFromFormat('d/m/Y', $date1) -> getTimestamp();
$date2 = (isset($date2)) ? $dt = DateTime::createFromFormat($format, $date2) -> getTimestamp() : time();
return ($date1 <= $date2) ? true : false;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment