3v4l.org

run code in 300+ PHP versions simultaneously
<?php $fillStart = new DateTime("2019-07-23 00:15:00"); $fillEnd = new DateTime("2019-09-23 13:00:00"); $gapStart = new DateTime("2019-05-23 00:15:00"); $gapEnd = new DateTime("2019-06-23 13:00:00"); // // being with the original start $fillStartModified = clone $fillStart; // move to the next weekday and time $wdtStart = $gapStart->format("l H:i:s"); if ($fillStartModified->format("l H:i:s") != $wdtStart) { $fillStartModified->modify("next {$wdtStart}"); } // get the duration, minus a bit so we can do a "next <day>" after $days = $gapStart->diff($gapEnd)->days - 1; // get the weekday and time $wdtEnd = $gapEnd->format("l H:i:s"); // update $fillEndModified = clone $fillStartModified; $fillEndModified->modify("+${days} days"); $fillEndModified->modify("next {$wdtEnd}"); $f = "l Y-m-d H:i:s"; printf("Fill %28s -> %28s\n", $fillStart->format($f), $fillEnd->format($f)); printf("Gap %28s -> %28s\n", $gapStart->format($f), $gapEnd->format($f)); printf("New %28s -> %28s\n", $fillStartModified->format($f), $fillEndModified->format($f));

preferences:
51.05 ms | 402 KiB | 5 Q