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!
The second kludge, as you call it, almost made my head ache! I think you should simplify that code ASAP
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:
$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');
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.
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?
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.
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.
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.