In this tutorial, I'll show you how to get the first and last day of the week in PHP.
By default, most PHP date calculations treat Monday as the start of the week and Sunday as the end. However, some systems consider Sunday as the first day. In this guide, we’ll cover both approaches.
We will use the following built-in PHP functions:
- date() – formats a timestamp into a readable date
- strtotime() – converts relative text into a timestamp
By combining these two functions, you can easily calculate the start and end of the current week.
How to get the first day of the week in PHP
If you want to get Sunday as the first day of the week, use the following method:
If you want to get Monday as the first day of the current week, use this method:
Note: Using this week is more reliable than -1 week, which can sometimes return unexpected results depending on the current day.
How to get the last day of the week in PHP
The following code will give you Saturday as the last day of the week (when Sunday is treated as the first day):
To get Sunday as the last day of the current week (when Monday is treated as the first day):
Get both start and end of the week (recommended)
If you need both values together, you can calculate them like this:
Using DateTime (more flexible approach)
For more complex applications, PHP’s DateTime class is a better option:
Set timezone (important)
Always make sure your timezone is set correctly to avoid incorrect dates:
Summary
- Use
monday this weekfor week start - Use
sunday this weekfor week end - Use
sunday last weekif Sunday is your week start - Use
DateTimefor more flexibility
Using the above methods you can get any day of the week and format it however you like.
I hope you found this helpful. All the best as you build your project.
Comments