Hi! Could we please enable some services and cookies to improve your experience and our website?

PHPize Online / SQLize Online  /  SQLtest Online

A A A
Login    Share code      Blog   FAQ

Online Sandbox for SQL and PHP: Write, Run, Test, and Share SQL Queries and PHP Code

Copy Format Clear

Stuck with a problem? Got Error? Ask AI support!

Copy Clear
Copy Format Clear
<?php function pushEveryThreeMonths($originalArray) { $newArray = []; echo $interval = new DateInterval('P3M'); echo $startDate = new DateTime(); // current date and time foreach ($originalArray as $element) { $newArray[] = $element; // push the element to the new array // Add three months to the start date $startDate->add($interval); // Check if the start date is in the future if ($startDate > new DateTime()) { break; // exit the loop if it's in the future } } return $newArray; } // Example usage: $originalArray = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J']; $newArray = pushEveryThreeMonths($originalArray); // Output the new array print_r($newArray);
Copy Clear