3v4l.org

run code in 300+ PHP versions simultaneously
<?php function last_monday($date) { if (!is_numeric($date)) $date = strtotime($date); if (date('w', $date) == 1) return $date; else return strtotime( 'last monday', $date ); } echo date('m/d/y', last_monday('8/14/2012')); // 8/13/2012 (tuesday gives us the previous monday) echo date('m/d/y', last_monday('8/13/2012')); // 8/13/2012 (monday throws back that day) echo date('m/d/y', last_monday('8/12/2012')); // 8/06/2012 (sunday goes to previous week)

preferences:
40.36 ms | 402 KiB | 5 Q