Search code examples
phpstringdatestrtotimeweek-number

what is the most efficient way to get 1 or 2 day before the first day of week number and year in php with native functions


I need to use php to return the Saturday or Sunday before the given week number and year, for example:

date('Y-m-d',strtotime("2011W011")) #returns '2011-01-03', but I need it to return '2011-01-01 or 2011-01-02 instead
 date('Y-m-d',strtotime("2011W014")) #returns '2011-01-06'

something like:

date('Y-m-d',strtotime("2011W01-2")) #to return '2011-01-01'
date('Y-m-d',strtotime("2012W01-2")) #to return '2011-12-31'    

I am hoping for something simple & efficient. Please do not suggests DateTime class...because my php dont have it, need a simple solution for a simple task.


Solution

  • strtotime('2012W01 - 2 days') for Saturday and strtotime('2012W01 - 1 day') for Sunday since the week call is always going to return you a Monday.