Year and week to date in php

前端 未结 4 803
孤独总比滥情好
孤独总比滥情好 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:00

    Try this out:

    $year = 2000;
    $week = 1;
    $start = date("l, M jS, Y", strtotime("01 Jan ".$year." 00:00:00 GMT + ".$week." weeks"));
    $end = date("l, M jS, Y", strtotime($start." + 1 week"));
    echo $start." to ".$end;
    

    You need to set $year and $week. It will then print the interval as specified.

    For instance, the output as-is is:

    Friday, Jan 7th, 2000 to Friday, Jan 14th, 2000
    

    Note that weeks are indexed from 0-51 (easy to fix).

    It's kind of ugly, but it works. Hope that helps!

    0 讨论(0)
  • 2020-12-15 07:03
    $year = "2010"; // Year 2010
    $week = "01"; // Week 1
    
    $date1 = date( "l, M jS, Y", strtotime($year."W".$week."1") ); // First day of week
    $date2 = date( "l, M jS, Y", strtotime($year."W".$week."7") ); // Last day of week
    echo $date1 . " - " . $date2;
    

    If week number is under 10 then append a 0 before number. 1 won't work, it should be 01.

    0 讨论(0)
  • 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..

    0 讨论(0)
  • 2020-12-15 07:17
    function getStartAndEndDate($week, $year) 
    {
        //setting the default time zone
        date_default_timezone_set('America/New_York');
    
        //getting the
        //$firstWeek = date('W',strtotime("January 1 $year", date(time())));
        //echo "Year : ".$year."<br/>"."Week : ".$week."<br/>";
        $firstWeekThursDay = date('W',strtotime("January $year first thursday",date(time())));
    
        if($firstWeekThursDay == "01")
        {
            $time      = strtotime("January $year first thursday",date(time()));
            //echo $time."<br/>";
            //echo date('Y-m-d H:i:s',$time)."<br/>";
            $time      = ($time-(4*24*3600))+(((7*$week)-6)*24*3600);
            //echo $time."<br/>";
            //echo date('Y-m-d H:i:s',$time)."<br/>";
            $return[0] = date('Y-m-d', $time);
            $time += 6*24*3600;
            $return[1] = date('Y-m-d', $time);
            //print_r($return);
        }
        else 
        {
            $time = strtotime("January 1 $year", time());
            //echo "<br/>".$time."<br/>";
            //echo date('Y-m-d H:i:s',$time)."<br/>";
            $time      = ($time-(4*24*3600))+(((7*$week)-6)*24*3600);
            //echo $time."<br/>";
            //echo date('Y-m-d H:i:s',$time)."<br/>";
            $return[0] = date('Y-m-d', $time);
            $time     += 6*24*3600;
            $return[1] = date('Y-m-d', $time);
            //print_r($return);
            //echo "<br/>End of Hi<br/>";
    
        }
        return $return;
    }
    
    0 讨论(0)
提交回复
热议问题