3v4l.org

run code in 300+ PHP versions simultaneously
<?php function closestDate($day){ $day = ucfirst($day); if(date('l', time()) == $day) return date("Y-m-d", time()); else if(abs(time()-strtotime('next '.$day)) < abs(time()-strtotime('last '.$day))) return date("Y-m-d", strtotime('next '.$day)); else return date("Y-m-d", strtotime('last '.$day)); } echo closestDate('Monday');

preferences:
31.8 ms | 402 KiB | 5 Q