// Check if a date field has a between validation if (substr($key, 0 , 6) == "DTBTW_") { //explode the value to get the dates between and get the field name $datevar = explode(",", $val); $requiredfield = substr($key, 6, strlen($key));
// work out the start and end dates and convert them to string $startdate = $datevar[0];
// Work out if the end date is today or a fixed date if ($datevar[1] == "today") { $enddate = date("j/n/Y"); } else { $enddate = $datevar[1]; }
// Now work out if we need to substract any dates from the end date $enddateArr = explode("/",$enddate); if ($datevar[4] == "-") { if ($datevar[3] == "day") { $enddate = date("j/n/Y", mktime(0,0,0, $enddateArr[1], ($enddateArr[0]-$datevar[2]), $enddateArr[2])); } elseif ($datevar[3] == "month") { $enddate = date("j/n/Y", mktime(0,0,0, $enddateArr[1]-$datevar[2], ($enddateArr[0]), $enddateArr[2])); } elseif ($datevar[3] == "year") { $enddate = date("j/n/Y", mktime(0,0,0, $enddateArr[1], ($enddateArr[0]), $enddateArr[2]-$datevar[2])); } } elseif ($datevar[4] == "+") { if ($datevar[3] == "day") { $enddate = date("j/n/Y", mktime(0,0,0, $enddateArr[1], ($enddateArr[0]+$datevar[2]), $enddateArr[2])); } elseif ($datevar[3] == "month") { $enddate = date("j/n/Y", mktime(0,0,0, $enddateArr[1]+$datevar[2], ($enddateArr[0]), $enddateArr[2])); } elseif ($datevar[3] == "year") { $enddate = date("j/n/Y", mktime(0,0,0, $enddateArr[1], ($enddateArr[0]), $enddateArr[2]+$datevar[2])); } }
echo strtotime($_POST[$requiredfield]) . ","; echo strtotime($startdate) . ","; echo strtotime($enddate) . ";"; // END TEST
// Now check that the date in the date field is between startdate and enddate THIS IS NOT CORRECT STILL WORKING IT OUT if((time($_POST[$requiredfield]) < time($startdate)) && (time($_POST[$requiredfield]) < time($enddate))) { $errormsg[$requiredfield] = "<span class=\"VAL_error\">This entry must be between " . $stardate . " and " . $enddate . ".</span>"; } }
If i enter in the form my date in uk style then it cannot convert that either.
I strongly recommend that you have the form use separate fields for month, day, and year. They can each be a select elements or text input elements, whichever you prefer, but this will get rid of any possible ambiguity as to what format is being used. You can then just use them in a mktime() to get the UNIX timestamp integer.
"Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
~ Terry Pratchett in Nation
This code is to validate that the date they select is between the startdate and enddate I tell it. How the user inputs it fairly irelevant at this point. Although I will say I have taken it on board and have done it but I still have this problem.
I don't think UNIX timestamps will go back as far as 1900. Assuming timestamps are being represented as signed 32-bit integers, positive values go from 1970-01-01 to some time in 2038 (after which we all better be using 64-bit representations). If I correctly did the math in my head, the farthest back you can go with a negative value would be some time around 1902.
"Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
~ Terry Pratchett in Nation
PS: If you have lots of "date arithmetic" challenges or need to deal with a wider range of dates than supported by the standard date/time functions, you might want to look into the PEAR Date package for enhanced capabilities.
"Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
~ Terry Pratchett in Nation
// Check if a date field has a between validation if (substr($key, 0 , 6) == "DTBTW_") { //explode the value to get the dates between and get the field name $datevar = explode(",", $val); $requiredfield = substr($key, 6, strlen($key));
// Work out if the end date is today or a fixed date if ($datevar[0] == "today") { $startdateArr = explode("/",date("j/n/Y")); } else { $startdateArr = explode("/",$datevar[0]); }
// Work out if the end date is today or a fixed date if ($datevar[1] == "today") { $enddateArr = explode("/",date("j/n/Y")); } else { $enddateArr = explode("/",$datevar[1]); }
// Now work out if we need to substract any dates from the end date if ($datevar[4] == "-") { if ($datevar[3] == "day") { $enddate = mktime(0,0,0, $enddateArr[1], ($enddateArr[0]-$datevar[2]), $enddateArr[2]); } elseif ($datevar[3] == "month") { $enddate = mktime(0,0,0, $enddateArr[1]-$datevar[2], ($enddateArr[0]), $enddateArr[2]); } elseif ($datevar[3] == "year") { $enddate = mktime(0,0,0, $enddateArr[1], ($enddateArr[0]), $enddateArr[2]-$datevar[2]); } } elseif ($datevar[4] == "+") { if ($datevar[3] == "day") { $enddate = mktime(0,0,0, $enddateArr[1], ($enddateArr[0]+$datevar[2]), $enddateArr[2]); } elseif ($datevar[3] == "month") { $enddate = mktime(0,0,0, $enddateArr[1]+$datevar[2], ($enddateArr[0]), $enddateArr[2]); } elseif ($datevar[3] == "year") { $enddate = mktime(0,0,0, $enddateArr[1], ($enddateArr[0]), $enddateArr[2]+$datevar[2]); } }
// Create unix time stamp for the field value $requiredvalueArr = explode("/", $_POST[$requiredfield]); $requiredvalue = mktime(0,0,0,$requiredvalueArr[1], $requiredvalueArr[0], $requiredvalueArr[2]);
// Now check that the date in the date field is between startdate and enddate if(($requiredvalue < $startdate) || ($requiredvalue > $enddate)) { $errormsg[$requiredfield] = "<span class=\"VAL_error\">This entry must be between " . date("j/n/Y",$startdate) . " and " . date("j/n/Y", $enddate) . ".</span>"; } }
Just note that the start date must be from year 1902.
Bookmarks