Skip to main content

Php date: How to get the first and last day of the week

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:

PHP

$firstday = date('l Y-m-d', strtotime("sunday last week"));
echo $firstday;
  

If you want to get Monday as the first day of the current week, use this method:

PHP

$monday = date('l Y-m-d', strtotime("monday this week"));
echo $monday;
  

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):

PHP

$lastday = date('l Y-m-d', strtotime("saturday this week"));
echo $lastday;
  

To get Sunday as the last day of the current week (when Monday is treated as the first day):

PHP

$sunday = date('l Y-m-d', strtotime("sunday this week"));
echo $sunday;
  

Get both start and end of the week (recommended)

If you need both values together, you can calculate them like this:

PHP

$start = date('Y-m-d', strtotime("monday this week"));
$end = date('Y-m-d', strtotime("sunday this week"));

echo "Start of week: " . $start;
echo "End of week: " . $end;
  

Using DateTime (more flexible approach)

For more complex applications, PHP’s DateTime class is a better option:

PHP

$today = new DateTime();

$startOfWeek = clone $today;
$startOfWeek->modify('monday this week');

$endOfWeek = clone $today;
$endOfWeek->modify('sunday this week');

echo $startOfWeek->format('Y-m-d');
echo $endOfWeek->format('Y-m-d');
  

Set timezone (important)

Always make sure your timezone is set correctly to avoid incorrect dates:

PHP

date_default_timezone_set('Africa/Harare');
  

Summary

  • Use monday this week for week start
  • Use sunday this week for week end
  • Use sunday last week if Sunday is your week start
  • Use DateTime for 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

Popular posts from this blog

How to easily create background notifications in Sketchware(original)

How to easily create background notifications in Sketchware(original) One of the keys to building a successful app is to find mechanisms that will keep your users engaged. You can do this by using background notifications. This tutorial will show you how to do that in Sketchware. We will cover: 1. How to create notifications in Sketchware 2. How to show these notifications even when the app is closed.

Happy Birthday Memes: Laugh Your Way to Someone's Heart

Happy Birthday Memes: Laugh Your Way to Someone's Heart Introduction Birthdays are a special occasion to celebrate another year of life, and what better way to do it than with a good laugh? That's where happy birthday memes come in! These hilarious images are the perfect way to put a smile on someone's face and let them know you're thinking of them on their special day.  

Cybersecurity Essentials: How To Stay Safe On The Internet

  Hi there, my name's Likhwa and welcome to Building Africa's Next Tech Unicorn. on this platform I give a behind the scenes look at what it takes to build a startup within the African context. In today's video, I'll be doing something different- I'll be talking about how you can stay safe online. This isn't meant to scare you or get you all paranoid, but to help you be more vigilant in protecting yourself and your loved ones from cyber threats.  Most people underestimate the dangers of the internet, but the more I am exposed to it, the more I wish I didn't need it. At times I wish I could just disconnect, get rid of all my devices and not have to worry about the constant threats that come with being online. I believe some of us are fortunate by default- especially those who live in countries like my Zimbabwe- because we hardly use credit cards to transact online and even when we do, its not like we have a lot of money- otherwise we would be a hacker's g...