Calculating days of week given a week number

2019-01-04 00:50发布

Given a week number, e.g. date -u +%W, how do you calculate the days in that week starting from Monday?

Example rfc-3339 output for week 40:

2008-10-06
2008-10-07
2008-10-08
2008-10-09
2008-10-10
2008-10-11
2008-10-12

标签: php date
13条回答
Explosion°爆炸
2楼-- · 2019-01-04 01:27
    <?php
    $iWeeksAgo = 5;// need weeks ago
    $sWeekDayStartOn = 0;// 0 - Sunday, 1 - Monday, 2 - Tuesday
    $aWeeksDetails = getWeekDetails($iWeeksAgo, $sWeekDayStartOn);

    print_r($aWeeksDetails);
    die('end of line of getWeekDetails ');

    function getWeekDetails($iWeeksAgo, $sWeekDayStartOn){
        $date = new DateTime();
        $sCurrentDate = $date->format('W, Y-m-d, w');
        #echo 'Current Date (Week of the year, YYYY-MM-DD, day of week ): ' . $sCurrentDate . "\n";

        $iWeekOfTheYear = $date->format('W');// Week of the Year i.e. 19-Feb-2014 = 08
        $iDayOfWeek = $date->format('w');// day of week for the current month i.e. 19-Feb-2014 = 4
        $iDayOfMonth = $date->format('d'); // date of the month i.e. 19-Feb-2014 = 19

        $iNoDaysAdd = 6;// number of days adding to get last date of the week i.e. 19-Feb-2014  + 6 days = 25-Feb-2014

        $date->sub(new DateInterval("P{$iDayOfWeek}D"));// getting start date of the week
        $sStartDateOfWeek = $date->format('Y-m-d');// getting start date of the week

        $date->add(new DateInterval("P{$iNoDaysAdd}D"));// getting end date of the week
        $sEndDateOfWeek = $date->format('Y-m-d');// getting end date of the week

        $iWeekOfTheYearWeek = (string) $date->format('YW');//week of the year
        $iWeekOfTheYearWeekWithPeriod = (string) $date->format('Y-W');//week of the year with year

        //To check uncomment
        #echo "Start Date / End Date of Current week($iWeekOfTheYearWeek), week with - ($iWeekOfTheYearWeekWithPeriod) : " . $sStartDateOfWeek . ',' . $sEndDateOfWeek . "\n";

        $iDaysAgo = ($iWeeksAgo*7) + $iNoDaysAdd + $sWeekDayStartOn;// getting 4 weeks ago i.e. no. of days to substract

        $date->sub(new DateInterval("P{$iDaysAgo}D"));// getting 4 weeks ago i.e. no. of days to substract
        $sStartDateOfWeekAgo = $date->format('Y-m-d');// getting 4 weeks ago start date i.e. 19-Jan-2014

        $date->add(new DateInterval("P{$iNoDaysAdd}D")); // getting 4 weeks ago end date i.e. 25-Jan-2014
        $sEndDateOfWeekAgo = $date->format('Y-m-d');// getting 4 weeks ago start date i.e. 25-Jan-2014

        $iProccessedWeekAgoOfTheYear = (string) $date->format('YW');//ago week of the year
        $iProccessedWeekOfTheYearWeekAgo = (string) $date->format('YW');//ago week of the year with year
        $iProccessedWeekOfTheYearWeekWithPeriodAgo = (string) $date->format('Y-W');//ago week of the year with year

        //To check uncomment
        #echo "Start Date / End Date of week($iProccessedWeekOfTheYearWeekAgo), week with - ($iProccessedWeekOfTheYearWeekWithPeriodAgo) ago: " . $sStartDateOfWeekAgo . ',' . $sEndDateOfWeekAgo . "\n";

        $aWeeksDetails = array ('weeksago' => $iWeeksAgo, 'currentweek' => $iWeekOfTheYear, 'currentdate' => $sCurrentDate, 'startdateofcurrentweek' => $sStartDateOfWeek,  'enddateofcurrentweek' => $sEndDateOfWeek,
                                'weekagoyearweek' => $iProccessedWeekAgoOfTheYear, 'startdateofagoweek' => $sStartDateOfWeekAgo,  'enddateofagoweek' => $sEndDateOfWeekAgo);

        return $aWeeksDetails;
    }
?> 
查看更多
等我变得足够好
3楼-- · 2019-01-04 01:32
$week_number = 40;
$year = 2008;

for($day=1; $day<=7; $day++)
{
    echo date('m/d/Y', strtotime($year."W".$week_number.$day))."\n";
}

This will fail if $week_number is less than 10.

//============Try this================//

$week_number = 40;
$year = 2008;

if($week_number < 10){
   $week_number = "0".$week_number;
}

for($day=1; $day<=7; $day++)
{
    echo date('m/d/Y', strtotime($year."W".$week_number.$day))."\n";
}

//==============================//
查看更多
甜甜的少女心
4楼-- · 2019-01-04 01:32

I found a problem with this solution. I had to zero-pad the week number or else it was breaking.

My solution looks like this now:

$week_number = 40;
$year = 2008;
for($day=1; $day<=7; $day++)
{
    echo date('m/d/Y', strtotime($year."W".str_pad($week_number,2,'0',STR_PAD_LEFT).$day))."\n";
}
查看更多
霸刀☆藐视天下
5楼-- · 2019-01-04 01:33

If you've got Zend Framework you can use the Zend_Date class to do this:

require_once 'Zend/Date.php';

$date = new Zend_Date();
$date->setYear(2008)
     ->setWeek(40)
     ->setWeekDay(1);

$weekDates = array();

for ($day = 1; $day <= 7; $day++) {
    if ($day == 1) {
        // we're already at day 1
    }
    else {
        // get the next day in the week
        $date->addDay(1);
    }

    $weekDates[] = date('Y-m-d', $date->getTimestamp());
}

echo '<pre>';
print_r($weekDates);
echo '</pre>';
查看更多
Ridiculous、
6楼-- · 2019-01-04 01:35

PHP

$week_number = 40;
$year = 2008;
for($day=1; $day<=7; $day++)
{
    echo date('m/d/Y', strtotime($year."W".$week_number.$day))."\n";
}


Below post was because I was an idiot who didn't read the question properly, but will get the dates in a week starting from Monday, given the date, not the week number..

In PHP, adapted from this post on the PHP date manual page:

function week_from_monday($date) {
    // Assuming $date is in format DD-MM-YYYY
    list($day, $month, $year) = explode("-", $_REQUEST["date"]);

    // Get the weekday of the given date
    $wkday = date('l',mktime('0','0','0', $month, $day, $year));

    switch($wkday) {
        case 'Monday': $numDaysToMon = 0; break;
        case 'Tuesday': $numDaysToMon = 1; break;
        case 'Wednesday': $numDaysToMon = 2; break;
        case 'Thursday': $numDaysToMon = 3; break;
        case 'Friday': $numDaysToMon = 4; break;
        case 'Saturday': $numDaysToMon = 5; break;
        case 'Sunday': $numDaysToMon = 6; break;   
    }

    // Timestamp of the monday for that week
    $monday = mktime('0','0','0', $month, $day-$numDaysToMon, $year);

    $seconds_in_a_day = 86400;

    // Get date for 7 days from Monday (inclusive)
    for($i=0; $i<7; $i++)
    {
        $dates[$i] = date('Y-m-d',$monday+($seconds_in_a_day*$i));
    }

    return $dates;
}

Output from week_from_monday('07-10-2008') gives:

Array
(
    [0] => 2008-10-06
    [1] => 2008-10-07
    [2] => 2008-10-08
    [3] => 2008-10-09
    [4] => 2008-10-10
    [5] => 2008-10-11
    [6] => 2008-10-12
)
查看更多
Bombasti
7楼-- · 2019-01-04 01:35

Another solution:

//$date Date in week
//$start Week start (out)
//$end Week end (out)

function week_bounds($date, &$start, &$end) {
    $date = strtotime($date);
    $start = $date;
    while( date('w', $start)>1 ) {
        $start -= 86400;
    }
    $end = date('Y-m-d', $start + (6*86400) );
    $start = date('Y-m-d', $start);
}

Example:

week_bounds("2014/02/10", $start, $end);
echo $start."<br>".$end;

Out:

2014-02-10
2014-02-16
查看更多
登录 后发表回答