Generating a 'Y-m-d' value for the start of the previous week

• Oct 16, 2011, 03:10
Forbes
Generating a 'Y-m-d' value for the start of the previous week
Hi guys!

I have some code that calculates the date for the beginning of the previous week.

PHP Code:

``` date("Y-m-d", time() - (date('w') - 1) * (60 * 60 * 24))  ```
However, in MySQL, the date needs to be shifted back one day to grab the first day of the date.

Now, I have actually solved this problem.

PHP Code:

``` \$array_date = explode('-', date("Y-m-d", time() - (date('w') - 1) * (60 * 60 * 24)));\$array_search['start'] = date('Y-m-d', mktime(0, 0, 0, \$array_date[1], \$array_date[2] - 1, \$array_date[0]));  ```
But it's a kludge.

So this is more out of curiosity than anything else, I'd just like to know what would need to be done to the first code sample to add on that extra day at the beginning.

I've tried everything I can think of, so any solution would be much appreciated!
• Oct 16, 2011, 07:28
Lemon Juice
The second kludge, as you call it, almost made my head ache! I think you should simplify that code ASAP :D

Quote:

Originally Posted by Forbes
I have some code that calculates the date for the beginning of the previous week.

PHP Code:

``` date("Y-m-d", time() - (date('w') - 1) * (60 * 60 * 24))  ```

This doesn't seem to work as expected, today it's sunday 2011-10-16 and the code above returns 2011-10-17, which is monday tomorrow and not monday last week! It's because date('w')-1 equals -1, which means you are subtracting a negative number, which works like adding a positive number. These are pitfalls of using complicated code, I think using the DateTime object will make it much easier for you.

Do you consider Monday or Sunday to be the beginning of the week? I'll assume you want Monday, so here you have the code to grab the beginning of last (previous) week plus the day before:
PHP Code:

``` \$date = new DateTime('last Monday'); if (date('D') != 'Mon') {     // if today is not Monday, then go one week earlier     \$date->modify('-1 week'); } \$prev_week_start = \$date->format('Y-m-d'); \$date->modify('-1 day'); \$day_before = \$date->format('Y-m-d');  ```
Works in PHP 5.2+.
• Oct 16, 2011, 13:21
Forbes
Hi, I realised that the two dates would be the same at the beginning of the week, which is why I subtracted one day from the date for the starting date of the previous week. But that's not what I'm asking about; I want a more elegant way of calculating the starting date of the previous week that includes a subtraction of one day.

I'll have a play around with the date object and see if that simplifies things. As an aside, I'll probably be spinning the code off as a method in a helper class.

Thanks for the feedback.
• Oct 16, 2011, 14:06
Lemon Juice
Quote:

Originally Posted by Forbes
Hi, I realised that the two dates would be the same at the beginning of the week, which is why I subtracted one day from the date for the starting date of the previous week. But that's not what I'm asking about;

Okay, I just commented on that code because when run on a Sunday, it adds one day instead of subtracting, which is why it returns the date for tomorrow. Because in that case it doesn't do what you said it does I decided to commend on that as well :D
Quote:

I want a more elegant way of calculating the starting date of the previous week that includes a subtraction of one day.
Isn't that what I included in the last 2 lines of my sample code? Or maybe I don't fully understand what you are looking for?
• Oct 17, 2011, 06:45
StarLion
date('Y-m-d',strtotime('previous week monday'));

http://www.php.net/manual/en/datetim...s.relative.php
• Oct 17, 2011, 06:58
Lemon Juice
Quote:

Originally Posted by StarLion
date('Y-m-d',strtotime('previous week monday'));

This will work only on Mondays. If it's Tuesday it will return date for Monday yesterday, the OP wanted Monday of the previous week. It's illogical but here 'previous week monday' means simply 'last Monday'. The date/time parser seems to lack intelligence in this case.
• Oct 17, 2011, 07:03
StarLion
Quote:

Originally Posted by Lemon Juice
This will work only on Mondays. If it's Tuesday it will return date for Monday yesterday, the OP wanted Monday of the previous week. It's illogical but here 'previous week monday' means simply 'last Monday'. The date/time parser seems to lack intelligence in this case.

False.
I just changed my system clock to Tuesday, the 18th, and ran this:
PHP Code:

``` <?phpecho date('Y-m-d',strtotime('previous week monday'))."<br>";echo date('Y-m-d');?> ```
And got out:
2011-10-10
2011-10-18

So... it DOES have intelligence.

If i had said "previous monday", it would have returned the 17th; but throwing Week in changes the statement.
• Oct 17, 2011, 07:12
Lemon Juice
Quote:

Originally Posted by StarLion
False.
I just changed my system clock to Tuesday, the 18th, and ran this:
PHP Code:

``` <?php echo date('Y-m-d',strtotime('previous week monday'))."<br>"; echo date('Y-m-d'); ?> ```
And got out:
2011-10-10
2011-10-18

That's interesting because I could say false to your false! On my computer when I set the clock to the 18th I get this:

2011-10-17
2011-10-18

This is on Windows, PHP 5.2.17. What version are you using? Maybe it's a bug that has been fixed.
• Oct 17, 2011, 07:14
StarLion
XAMPP, Windows, PHP v5.3.5. So perhaps the 5.3 branch has more logic in it.
• Oct 17, 2011, 07:30
Lemon Juice
So 5.3 must be more logical. Still, I would advise to test this for all days of the week. "previous week" may mean something different depending on which day the parser considers to be the beginning of the week: it could be Monday or Sunday. If it's Sunday the results can be unexpected. And if the day which begins the week depends on the system locale then this would make this code bad for portability - I'm just speculating, someone would need to confirm how the parser works. If I'm not sure I would prefer not to rely on some obscure intelligence deciding what 'previous week' means.
• Oct 17, 2011, 09:41
Forbes
Okay, in light of the possible discrepancies (and this thread has proved very interesting), I'll keep the previous code and use the code by StarLion for now, pending any weirdness.

Guys, thanks!