13 November 2023

PHP inherently supports ISO date format. With slight modifications we can still detect the start and end days of the week.

Source code viewer
  1. // 0 - Sunday, 1 - Monday
  2. $week_start_day = 0;
  3.  
  4. $date_time_from = new DateTime('now');
  5. $date_time_to = clone $date_time_from;
  6.  
  7. $this_week_first_day =
  8. $date_time_from->setISODate(
  9. $date_time_from->format('Y'),
  10. $date_time_from->format('W'),
  11. $week_start_day
  12. );
  13. $this_week_last_day =
  14. $date_time_to->setISODate(
  15. $date_time_to->format('Y'),
  16. $date_time_to->format('W'),
  17. (6 + $week_start_day)
  18. );
  19.  
  20. // Set time to the start of day and end of day, correspondingly.
  21. $this_week_first_day->modify('00:00:00.000000');
  22. $this_week_last_day->modify('23:59:59.999999');
Programming Language: PHP