First date of a week

后端 未结 4 1113
深忆病人
深忆病人 2021-01-06 13:49

I want a First Day Of a week say I have 45th week. Now I want to have date of the first sunday of this week. Can any one suggest me how I can go about this ?

Thank

4条回答
  •  难免孤独
    2021-01-06 14:16

    Searching around a bit, the most suggested is:

    $year = "2009"; // date("Y");
    $week = "45"; // date("W");
    
    $firstDayOfWeek = strtotime($year."W".str_pad($week,2,"0",STR_PAD_LEFT));
    
    print("The first day of week ".$week." of ".$year." is ".date("D, d-m-Y",$firstDayOfWeek));
    

    Basically this comes down to letting strtotime do the work for you. You fill in "2009W45" and it'll retrieve the date for you. Pitfall here is that the week needs to be in a 2 digit format. So week 1 needs to be 01, hence the str_pad to zero-fill it.

提交回复
热议问题