3v4l.org

run code in 300+ PHP versions simultaneously
<?php $fillStart = new DateTime("2020-09-27 12:34:56"); $fillEnd = new DateTime("2020-10-10 12:34:56"); $gapStart = new DateTime("2020-09-09 01:02:03"); $gapEnd = new DateTime("2020-09-11 04:05:06"); // // 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 %30s -> %30s\n", $fillStart->format($f), $fillEnd->format($f)); printf("Gap %30s -> %30s\n", $gapStart->format($f), $gapEnd->format($f)); printf("New %30s -> %30s\n", $fillStartModified->format($f), $fillEndModified->format($f));

preferences:
48.88 ms | 402 KiB | 5 Q