3. [20 pts] PHP This question is a continuation of question 2., but from the ser
ID: 3843629 • Letter: 3
Question
3. [20 pts] PHP
This question is a continuation of question 2., but from the server side.
a) Write a PHP snippet that extracts the two dates from the incoming data and store them in variables $date1 and $date2.
b) Create an array called $dates and fill it with 5 random dates in the range between $date1 and $date2 inclusive. The dates must be valid!! Hint #1: Recall that to get random integer numbers in the range [x,y] use predefined function mt_rand(x,y); Hint #2: If you are thinking that this problem does not need timestamps, you are wrong. Think again.
c) Write a PHP snippet that creates a table of the 5 random dates you have generated. Hint: date("d/m/Y", $ts); will be helpful.
Explanation / Answer
function createDateRangeArray($strDateFrom,$strDateTo) { // takes two dates formatted as YYYY-MM-DD and creates an // inclusive array of the dates between the from and to dates. // could test validity of dates here but I'm already doing // that in the main script $aryRange=array(); $iDateFrom=mktime(1,0,0,substr($strDateFrom,5,2), substr($strDateFrom,8,2),substr($strDateFrom,0,4)); $iDateTo=mktime(1,0,0,substr($strDateTo,5,2), substr($strDateTo,8,2),substr($strDateTo,0,4)); if ($iDateTo>=$iDateFrom) { array_push($aryRange,date('Y-m-d',$iDateFrom)); // first entry while ($iDateFromRelated Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.