get first day of this week in php

To get the first day of the current week in PHP, you can use the DateTime class along with the format method to get the day of the week and then subtract the appropriate number of days to get to the beginning of the week.

Here's an example code snippet that gets the first day of the week:

main.php
$now = new DateTime(); // current date and time
$weekStart = $now->modify('this week')->format('Y-m-d');
105 chars
3 lines

This code first creates a DateTime object representing the current date and time. Then, it uses the modify method to set the DateTime to the beginning of the current week. Finally, it uses the format method to format the DateTime object as a string in the format YYYY-MM-DD, representing the first day of the current week.

Alternatively, if you want to get the name of the day instead of the date, you can modify the code like this:

main.php
$now = new DateTime(); // current date and time
$weekStart = $now->modify('this week')->format('l');
101 chars
3 lines

This will get the name of the day (e.g. "Monday") instead of the date in the format YYYY-MM-DD.

gistlibby LogSnag