Hello! I have a variable $ node-> field_work_start [0] ['view'] which denotes the date of birth. To follow up I want to do a background check on the fact that this date is included in the next period then 10 days from the current date. If true then x = 1, false x = 0. Help please with code PHP.
A:
It sounds like you are given a DATETIME with $node->field_work_start[0]['view'] correct? If this is the case, we're going to convert it to a UNIX timestamp to make things a little bit easier. In this example we will assume your date is stored in M d, Y form.
This little code should work well for you!
<?php
class Node
{
// For sake of keeping things similar to your 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 range
for($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);
?>
It should return this output:
January 1, 1970 is not within the 10 day range.
July 20, 1970 is within the 10 day range.
Good luck!
Regards,
Dennis M.
RageD
2010-07-12 01:40:50
Thank you for your reply.variable $node->field_work_start[0]['view'] can be simply reduced to the form$born = $node->field_work_start[0]['view'] and work further with the variable $born.Perhaps the task simpler, but I do not have the output format of 09.15.1974 and not quite correctly raised the question.
Siteograf
2010-07-12 10:06:57
Ok, so assuming $born is your variable now, you can use this function with any format of date. You just need to convert it before sending it to the function. So if you have some other formatting for your date (as long as it is DATETIME and not UNIX), you can simply put this into the function: checkBirthday(date("M d, Y"),strtotime($born)),10); and that will work. If it is in UNIX time, remove the strtotime() tags from around $born in that line.
RageD
2010-07-12 14:39:15