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

1. ## 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!

2. The second kludge, as you call it, almost made my head ache! I think you should simplify that code ASAP

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+.

3. 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.

4. 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
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?

5. date('Y-m-d',strtotime('previous week monday'));

http://www.php.net/manual/en/datetim...s.relative.php

6. 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.

7. 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.

8. 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.

9. XAMPP, Windows, PHP v5.3.5. So perhaps the 5.3 branch has more logic in it.

10. 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!