Year and week to date in php

前端 未结 4 807
孤独总比滥情好
孤独总比滥情好 2020-12-15 06:24

I have two pieces of information extracted from a MySQL database, the year(2009, 2010, ect) and the week (1-52). And I need to convert it i

4条回答
  •  攒了一身酷
    2020-12-15 07:09

    Since this question and the accepted answer were posted the DateTime classes make this much simpler to do:-

    function daysInWeek($weekNum)
    {
        $result = array();
        $datetime = new DateTime();
        $datetime->setISODate((int)$datetime->format('o'), $weekNum, 1);
        $interval = new DateInterval('P1D');
        $week = new DatePeriod($datetime, $interval, 6);
    
        foreach($week as $day){
            $result[] = $day->format('d/m/Y');
        }
        return $result;
    }
    
    var_dump(daysInWeek(24));
    

    Output:-

    array (size=7)
      0 => string '10/06/2013' (length=10)
      1 => string '11/06/2013' (length=10)
      2 => string '12/06/2013' (length=10)
      3 => string '13/06/2013' (length=10)
      4 => string '14/06/2013' (length=10)
      5 => string '15/06/2013' (length=10)
      6 => string '16/06/2013' (length=10)
    

    This has the added advantage of taking care of leap years etc..

提交回复
热议问题