0

I have an array of dates that is filled with string formated days sorted in descending order varying one another 30 days. The 31-days months are already converted in 30-days months.

$monthdates = array('2001-29-05','2001-29-06','2001-29-07','2001-29-08','2001-29-09','2001-29-10');

I also have another array containing string formated days in descending order that are ranged within the dates in the above array like this:

$customdates = array('2001-30-05','2001-31-05','2001-01-06','2001-02-06',2001-03-06);

...say this array last value is '2001-29-11'.

What I want is inside a loop (or more likely two) to traverse the arrays and compare the $monthdates' current day with the day in the $customdates array and echo something (whatever) IF the day in the $customdates array is more recent and exactly 20 days than the day in the $monthdates array.

For example: '2001-30-05' in the $customdates array is more recent than '2001-29-05' in the $monthdates array. In that case, we should figure out these two days' numeric difference and if it exceeds 20 days(varying one another 20 or more days), then print a random string, while keep doing the above comparison FOR the every next value in the $customdates array.

If a "match" found for the $monthdates array, stop doing the comparison for its current value and move on to the next value. In our example the '2001-30-05' and the '2001-29-05' differ only 1 day. When we find a match for every value in the $monthdates array, get its next value.

Ricardus
  • 739
  • 2
  • 8
  • 15
  • 4
    convert dates to timestamp then substract them and see the difference in seconds. – Robert Jun 03 '13 at 22:39
  • If you use that notation, people (and so does PHP) expect you to use the full ISO format, so it would be YYYY-MM-DD and NOT YYYY-DD-MM... If you do not reverse the dates, codes like @SH-'s will produce unexpected results – ItalyPaleAle Jun 05 '13 at 22:36

1 Answers1

2

Using DateDiff and DateInterval to compare the two strings. Read till we find a match. Then we can break. If no match we hit the end and start the next one anyway.

foreach($monthdates as $date){
     $dateObject = date_create($date);
     foreach ($customdates as $custDate){
         $dateCustomObject = date_create($custDate);
         date_diff($dateCustomObject, $dateObject)->days >= 20 ?
             substr(str_shuffle("0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"), 0, 15) :
             break;
     }
}

PS: I didn't write the print random string part. Took it from this SO question

Community
  • 1
  • 1
SH-
  • 1,642
  • 10
  • 13