• 0

[PHP] Find start and end day of current week


Question

Is there a PHP code to set the start of a week to Saturday, find the date of that, and set the end of a week to Friday and find the date of that?

For example:

The start of this week is Feb 20, 2010 and the end of this week is Feb 26, 2010.

Link to comment
Share on other sites

2 answers to this question

Recommended Posts

  • 1

Have a look at strtotime. It accepts many different formats and it can even do things like "last Saturday" or "next Friday". However, this is not 100% the way we want it: when the current date is a Friday, we want the start date to be that Friday and not the next. Therefore, we need a check on the day of the week first to make sure we always get the correct week boundaries.

<?php
$date = time(); // Change to whatever date you need
$dotw = $dotw = date('w', $date);
$start = ($dotw == 6 /* Saturday */) ? $date : strtotime('last Saturday', $date);
$end = ($dotw == 5 /* Friday */) ? $date : strtotime('next Friday', $date);
?>

If you'd rather code this by hand, use date('w') to get the day of the week and add or subtract something to get the date. Of course here, you'll have to look out when you're near the beginning or end of the month. You can solve this however by doing your calculations on the UNIX timestamp itself instead which is nothing more than the amount of seconds since January 1st, 1970. Because PHP uses Sunday to Saturday as weekday bounds, we'll need to do some shifting. Where Monday is 1 in PHP, we need Monday to become 2, so we'll shift the day of the week one day forward. We also need to make sure that Saturday (6 in PHP) gets shifted to 0, so we check when the day exceeds 6 and subtract 7 if it does. The result is the amount of days since the last Saturday, and from here it's just a matter of subtracting it from the given date to retrieve the start date. To get the end date, we just add 6 days to the start date.

<?php
$date = time(); // Change to whatever date you need
// Get the day of the week: Sunday = 0 to Saturday = 6
$dotw = date('w', $date);
// Make it range between Saturday (= 0) and Friday (= 6) by adding one day (and subtracting a week when the result exceeds the week boundaries)
if(++$dotw > 6) $dotw -= 7;
// Calculate the days
$start = $date - ($dotw * 24*60*60); // Substract days for start of week
$end = $start + (6 * 24*60*60); // Add 6 days to the start to get the end of the week
?>

And there you have it! Two methods, both tested and confirmed to work on my PHP5 box. One using the date shifting methods provided by PHP itself (although this one requires the engine to parse the string first) and one using an handmade algorithm with just one call to PHP's date function. Choose whichever you like most! ;)

Edit: It appears that 'last Saturday' sets the time to 00:00:00 whereas 'next Friday' preserves the time of the input date. Therefore, if you need the time of the date later, I'd suggest either the second method, or this third method which is a combination of both: using strtotime to get the end date and then manually subtracting 6 days to get the start date.

<?php
$date = time(); // Change to whatever date you need
$dotw = $dotw = date('w', $date);
$end = ($dotw == 5 /* Friday */) ? $date : strtotime('next Friday', $date);
$start = $end - (6 * 24*60*60);
?>

Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.