Check a Date Exists Within a Given Period
So I recently was asked to solve a problem for someone trying to write a script to check if a birthday (presented in DATETIME M d, Y form) was present within a given period of days from the current date. Naturally, I feel inclined to give some assistance because I love working on these things.
So, I am posting this because I found it interesting. I feel like this is a very useful code snippet for anyone looking how to do this. The code is fairly well-documented, so I am not going to go through and explain all the facets of the code. However, I will make special mention of the real challenge to this. You have to make sure to account for the coming year if the date is after December 21 in any given year. This is because the system won’t automatically rollover to the next year and as you approach January 1 of the coming year, no dates will match until January 1 is met. So you can take a look in the code to see how I personally accomplished this task although there are probably hundreds of other ways to tackle the issue.
<?php
/**
* Birthday Script created by Dennis J. McWherter, Jr.
*
* (C) 2010 DENNIS J. MCWHERTER, JR. ALL RIGHTS RESERVED.
*/
class Node
{
// For sake of keeping things similar to a usable environment
var $field_work_start = array();
}/**
* checkBirthday function
*
* The function will check a birth date
* and see if it is in the range of specified
* days.
*
* @param $birthday
* Birth date parameter. Assumed to be in M d, Y form
* @param $days
* The range of days to check where the birthday is
* @return int
* 0 if the birthday is _NOT_ within the proper range
* 1 if the birthday _IS_ within the proper range
*/
function checkBirthday($birthday,$days)
{
// Parse out the year of the birthday
$pos = strpos($birthday,",")+2; // Find the comma which separates the year
$today = strtotime(date("M, d")); // Check if day is < Dec. 22. If not, we need to account for next year!// Check if the birthday is within the range and has not passed!
if($today < strtotime("Dec 22")){
$birthday = substr_replace($birthday,date("Y"),$pos,4); // Replace the year with current year
if(strtotime($birthday) <= strtotime("+".$days." days") && strtotime($birthday) >= time()){
return 1;
}
} else { // Less than 10 days left in our year.. check for January birthdays!
if(!strstr($birthday,"December")){
$birthday = substr_replace($birthday,date("Y")+1,$pos,4); // Replace the year with next year
$year = date("Y")+1;
} else { // Still December?
$birthday = substr_replace($birthday,date("Y"),$pos,4); // Replace the year with current year
$year = date("Y");
}
$day = (date("d")+10)-31; // 10 days from now is...if((strtotime($birthday) <= strtotime("January ".$day.", ".$year)) && strtotime($birthday) >= strtotime("December 25, 2010")){
return 1;
}
}
return 0;
}$node[] = new Node;
$node[0]->field_work_start[0] = "January 1, 1970"; // First birthday is _NOT_ within range
$node[1]->field_work_start[0] = "July 20, 1970"; // Second birthday _IS_ within rangefor($i=0;$i<count($node);$i++){
if(!checkBirthday($node[$i]->field_work_start[0],10)){
print $node[$i]->field_work_start[0]." is not within the 10 day range.<br /><br />";
} else {
print $node[$i]->field_work_start[0]." is within the 10 day range.<br /><br />";
}
}unset($node);
?>
Enjoy!
Regards,
Dennis M.