开发者

Find the date of a monday based on date of another day in php

I need to find a way to get the date of the monday of a week based on another day. So for example, if wednesday is 2011-05-11, i need to somehow tell it that monday of that week was 2011-05-09. Can anyone help?

$res looks like this:

stdClass Object
        (
            [link_count] => 1
            [day] => 2011-05-12
            [weekday] => Thu
        )

    $days = array(1=> 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun');
    foreach($res as $r) {
        $day = date('N', strtotime($r->day));
  开发者_开发问答      $r->weekday = $days[$day];
        if($r->weekday == 'Mon') {
            $r->weekstarting = $r->day;
        } else {
            // here's the problem right here, I need to find a way to tell it 
            // what it's starting day is
            $r->weekstarting = ;
        }

    }


Seems like you have the weekday (like 'Thu'), so you need to get is position in your $days array.

You can get the key with $key=array_search($r->weekday, $days); (returns 4).

Now you know the difference from Monday, so you just have to create the date:

$tempArray=explode('-', $r->day);
$tempArray[2]-=$key-1;
$r->weekstarting = implode('-', $tempArray);

EDIT: Now I see that you already had the array index in $day. So you could use that value instead of the $key I had.

EDIT 2: If you don't want to lose the leading zeros in the day, you could use sprintf() on it .

$tempArray=explode('-', $r->day);
$tempArray[2]=sprintf('%02d', $tempArray[2]-($key-1));
$r->weekstarting = implode('-', $tempArray);

And finally the LAST UPDATE. I was thinking about deleting the whole post and rewriting it again, but I will leave it here for reference... somebody might learn from my mistake.

So the final code that will correctly adjust months also:

list($y, $m, $d)=explode('-', $r->day);
$timestamp=mktime(0,0,0,$m,$d-($day-1),$y);
$r->weekstarting = date('Y-m-d', $timestamp);

Actually this is what I wanted to avoid, converting to a timestamp, but I could not. The trick I'm using here is that mktime() will handle negative numbers for days and calculate the timestamp correctly.


This seems to work fine:

<?php

$dt = new DateTime("11 May 2011");
$dt->modify("last monday");

edit: I remember seeing reports that some people had problems with the "last" modifier (windows users maybe) so this should work if you have problems with that:

<?php

$dt = new DateTime("12 May 2011");
$days = (int)$dt->format('N') - 1;
$dt->modify("-{$days} days");


Please create a testfile and execute it:

<?php
$dates = array(
    '11 May 2011',
    '2011-04-26',
    '2011-05-04',
);

foreach($dates as $date) {
    $dt = new DateTime($date);
    $dt->modify("last monday");
    printf("For %s last monday is %s.\n", $date, $dt->format('Y-m-d'));
}
?>

it should output the following:

For 11 May 2011 last monday is 2011-05-09.
For 2011-04-26 last monday is 2011-04-25.
For 2011-05-04 last monday is 2011-05-02.

In case not, please add your output to your question above.


I'd do something like this:

$dow = date('N', strtotime($r->day)) - 1; // 0 will be Monday, 1 Tuesday, etc.
$start = mktime() - ($dow * 86400); // 86400 is one day in seconds
$startday = date('r', $start);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜