tags:

views:

2332

answers:

7

I'm trying to get the week range using Sunday as the start date, and a reference date, say $date, but I just can't seem to figure it out.

For example, if I had $date as 2009-05-01, I would get 2009-04-26 and 2009-05-02. 2009-05-10 would yield 2009-05-10 and 2009-05-16. My current code looks like this (I can't remember where I lifted it from, as I forgot to put down the url in my comments):

function x_week_range(&$start_date, &$end_date, $date)
{
    $start_date = '';
    $end_date = '';
    $week = date('W', strtotime($date));
    $week = $week;

    $start_date = $date;

    $i = 0;
    while(date('W', strtotime("-$i day")) >= $week) {
     $start_date = date('Y-m-d', strtotime("-$i day"));
     $i++;
    }

    list($yr, $mo, $da) = explode('-', $start_date);

    $end_date = date('Y-m-d', mktime(0, 0, 0, $mo, $da + 6, $yr));
}

I realized all it did was add 7 days to the current date. How would you do this?

+8  A: 

I would take advantange of PHP's strtotime awesomeness:

function x_week_range(&$start_date, &$end_date, $date) {
    $ts = strtotime($date);
    $start = (date('w', $ts) == 0) ? $ts : strtotime('last sunday', $ts);
    $start_date = date('Y-m-d', $start);
    $end_date = date('Y-m-d', strtotime('next saturday', $start));
}

Tested on the data you provided and it works. I don't particularly like the whole reference thing you have going on, though. If this was my function, I'd have it be like this:

function x_week_range($date) {
    $ts = strtotime($date);
    $start = (date('w', $ts) == 0) ? $ts : strtotime('last sunday', $ts);
    return array(date('Y-m-d', $start),
                 date('Y-m-d', strtotime('next saturday', $start)));
}

And call it like this:

list($start_date, $end_date) = x_week_range('2009-05-10');

I'm not a big fan of doing math for things like this. Dates are tricky and I prefer to have PHP figure it out.

Paolo Bergantino
I used the following to get the last sunday date formatted:date('Y-m-d',strtotime('last sunday'))
John M
A: 

To be honest, I have trouble understanding the code you posted ;)

I guess something like this should do the trick:

function get_week($date) {
        $start = strtotime($date) - strftime('%w', $date) * 24 * 60 * 60;
        $end = $start + 6 * 24 * 60 * 60;
        return array('start' => strftime('%Y-%m-%d', $start),
                     'end' => strftime('%Y-%m-%d', $end));
}
rodion
A: 

Without doing so much string manipulation, you can do some simple math on the timestamps.

function getDateRange(&$start, &$end, $date) {
  $seconds_in_day = 86400;
  $day_of_week = date("w", $date); 
  $start = $date - ($day_of_week * $seconds_in_day);
  $end = $date + ((6 - $day_of_week) * $seconds_in_day);
}
Clay
+1  A: 

Here's my version, which uses a similar notion to Clay's:

/**
 * Start of the week
 *
 * 0 = Sun, 1 = Mon, etc.
 */
define( 'WEEK_START', 0 );

/**
 * Determine the start and end dates for
 * the week in which the specified date
 * falls
 *
 * @param $date Date in week
 * @param $start Week start (out)
 * @param $end Week end (out)
 */
function week_bounds( $date, &$start, &$end ) {
    $date = strtotime( $date );
    // Find the start of the week, working backwards
    $start = $date;
    while( date( 'w', $start ) > WEEK_START ) {
     $start -= 86400; // One day
    }
    // End of the week is simply 6 days from the start
    $end = date( 'Y-m-d', $start + ( 6 * 86400 ) );
    $start = date( 'Y-m-d', $start );
}

Lightly tested. Code may not be bulletproof, or handle dates in the far past or future. Use at your own risk. Do not immerse the code in water. Do not show the code to those of a nervous disposition, or with a hatred of the dollar sign. Not tested on animals. Safe for use by vegetarians. Author warrants that the code will never, ever speak to you. In the unlikely event that the code does try to engage you in conversation, the author advises you to disregard any and all advice it gives.

Rob
+1  A: 

Apparently 'w' formatting value of date() or the format method of a DateTime object will return the day of the week as a number (by default, 0=Sunday, 1=Monday, etc)

You could take this and subtract it's value as days from the current day to find the beginning of the week.

$start_date = new DateTime("2009-05-13");
$day_of_week = $start_date->format("w");

$start_date->modify("-$day_of_week day");

$start_date will now be equal to the Sunday of that week, from there you can add 7 days to get the end of the week or what-have-you.

Crazy Joe Malloy
A: 

Clay,

Something funny is happening in your function. If you use '2009-11-01' as the input date, the start date will be the 1st, but the END date will be reported as the 6th, not the 7th! In Paolo's function, it works fine! I really don't know why!

A: 

"I'm not a big fan of doing math for things like this. Dates are tricky and I prefer to have PHP figure it out." <= I love you so much, thanks you so much, your code is really cool ^^

bao nam