Skip to content

Instantly share code, notes, and snippets.

@gebeer
Created January 26, 2016 13:45
Show Gist options
  • Save gebeer/9774ca174cf3fb45780e to your computer and use it in GitHub Desktop.
Save gebeer/9774ca174cf3fb45780e to your computer and use it in GitHub Desktop.
check if a given date lies within a given month
// example code
// convert the saved date of the page to a timestamp
// $date = strtotime($page->date);
$date = strtotime("12-January-2016");
// convert the $searchdate to a timestamp
$searchdate = strtotime("January-2016") + 1000;
// get first and last timestamp of the given month and year
$getdate = getdate($searchdate);
// get the month number (e.g. 1 for January)
$month = $getdate["mon"];
// get the year (e.g. 2016
$year = $getdate["year"];
// get first timestamp of that month in that year
$first = mktime(0,0,0,$month,1,$year);
// echo date('r', $first);
// get last timestamp of that month in that year
$last = mktime(23,59,00,$month+1,0,$year);
// echo date('r', $last);
// now check if $date is in the range between $first and $last
function check_in_range($first, $last, $date)
{
// returns true if $date is in month January of year 2016 - else returns false
return (($date >= $first) && ($date <= $last));
}
var_dump(check_in_range($first, $last, $date));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment