You are senior programmer at GooBook and it is your responsibility to fix data c
ID: 3843638 • Letter: Y
Question
You are senior programmer at GooBook and it is your responsibility to fix data corrupted by a computer bug. The data is in a PHP array called dates. This array is indexed by keys. The dates array contains dates in the format MM-DD-YYYY e.g. 05-24-2013. The array might look something like: $dates = array("some_date" => "03/27/2012", "key2" => "04/02/1990", "some_other_date" = > "05/06/2007"); However, due to the corruption, each date is now off by 3 days. You need to fix each date by somehow adding 3 days to each date. Be careful! Note that you cannot simply add 3 days to each day slot. There may be carry over to a new month or even to a new year. Thus you have to come up with a uniform way of fixing the dates. Output each fixed date on a new line. The output for this example would be: 03/30/2012 04/05/1990 05/09/2007Explanation / Answer
$dateValues =array_values($dates);
for($1=0;$i<$sizeof($dateValues);$i++){
$date = $dateValues[i];
list($month, $day, $year) = split('[/.-]', $date);
$day = $day + 3;
if($day > 28){
if($month == 2){
$month = $month+1;
$day = $day-28;
}
elseif($month == 1 || $month == 3 || $month == 5 || $month == 7 || $month == 8 || $month == 10 || $month == 12){
$day = $day - 31;
$month = $month+1;
}
else{
$day=$day-30;
$month = $month+1;
}
}
if($month>12){
$year = $year+1;
$month = 1;
}
echo "$month/$day/$year";
}
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.