tags:

views:

182

answers:

4

Hi, In my application a week is defined from Monday 12:00:00 AM to Sunday 11:59:59 PM

Whenever a user visits my site - I need to find the previous weeks date range and show him results based on that. It sounds simple but I'm lost.

To give you scenarios - - March 1st Monday 12:00:00 AM to March 7th Sunday 12:59:59 PM is the week.

Now when a user visits the website on 8th March or 10th March or 12th March - based on the current date I should be able to get the previous week date range ie start date March 1st and end date March 7th.

But if the user visits the site say on 16th March - the date range I would need is March 8th to March 15th.

How can I do this in PHP. Thanks

+1  A: 
function get_week_start($year, $month, $day)
{
    $timestamp = mktime(0, 0, 0, $month, $day, $year);
    return date('F j Y', $timestamp = mktime(0, 0, 0, $month, date('d', $timestamp)-date('w', $timestamp), $year));
}

You could perhaps add the next 6 days and you have it.

Thorpe Obazee
Thanks Thorpe let me check it out
Gublooo
+3  A: 

You could try doing it with timestamps, but that gets messy with timezone changes (for example, CET -> CEST). I'd use the DateTime class:

$d = new DateTime();
$weekday = $d->format('w');
$diff = 7 + ($weekday == 0 ? 6 : $weekday - 1); // Monday=0, Sunday=6
$d->modify("-$diff day");
echo $d->format('Y-m-d') . ' - ';
$d->modify('+6 day');
echo $d->format('Y-m-d');
Lukáš Lalinský
Thanks Lukas - that works perfectly
Gublooo
To tidy up the day shifting, you could do`(6+$weekday)%7` instead of `($weekday == 0 ? 6 : $weekday - 1)`
rossmcf
+1  A: 

There is a user function for this in the PHP Documentation.

jasonbar
How did I miss that one :)- thanks
Gublooo
+2  A: 

The strtotime function is very handy here:

$mondayStr = "last monday";
if (date('N') !== '1') {  // it's not Monday today
    $mondayStr .= " last week";
}

$monday = strtotime($mondayStr);
echo date('r', $monday);    // Mon, 22 Feb 2010 00:00:00 +1000

$sunday = strtotime('next monday', $monday) - 1;
echo date('r', $sunday);    // Sun, 28 Feb 2010 23:59:59 +1000
nickf
thanks nick - i had not heard of this function before. Appreciate it
Gublooo
@Gublooo, there was a slight error with the code before: it would get *this* week, unless today was monday. Now it's fixed up.
nickf
This is exactly the kind of code I meant by "try it with timestamps" in my answer. If you live in the wrong country and run this on the wrong day, you will get a surprise, because the second result won't be 23:59:59 on Sunday, but 00:59:59 on Monday. Yay for DST. If you have to use timestamps, at least make sure you are using GMT.
Lukáš Lalinský
@Lukáš - I've changed the code now so that it will be resilient to DST changes. Also, timestamps are just a number - the number of seconds since midnight 1st Jan 1970 at GMT. As such, they don't have a timezone themselves.
nickf
They don't have a timezone, but they are bound to a timezone when you use `date()` to convert it to date/time. Different timezones will give you different conversions.
Lukáš Lalinský
it's still the exact same point in time though.
nickf