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.

How I got started with Sketchware

Background As I explored the Playstore sometime in June 2018, I stumbled upon an app to create other apps. Out of curiosity I downloaded it, did some research and played around with it. I soon realised I could actually create a “real” app from scratch and make money. See, some years earlier I had downloaded Android studio and done a few lessons on programming. So I had a basic understanding of Java and android programming, but this new app that I had found, used simple drag and drop features and you could actually run the app on your device or send your app to friends in just a few clicks. I was not a professional programmer but using this app I could easily create “proper” mobile apps without cracking my head over code.  You can check out my app made using Sketchware; Business Builder- Small Business Management Suite here . Fast forward to a few months later and I had created an app that I felt was ready for the big leagues… Play Store baby!! Note, that I was at a low point f...

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.