-4

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.

Cœur
  • 37,241
  • 25
  • 195
  • 267
xam
  • 452
  • 2
  • 7
  • 15
  • 1
    `strtotime('last saturday', strtotime('2011W01'))` – CBroe Jan 05 '15 at 20:14
  • `DateTime` is not a library; it's a class that's now part of PHP. It's there whether you use it or not. – nwk Jan 05 '15 at 20:18
  • I tried 'DateTime' and got an error of calling a not existed function. I am sure someone would suggests 'DateTime', and just want to let people know ahead of time. Thanks anyway – xam Jan 05 '15 at 20:25
  • @xam Try my or CBroe's comment answer, it works –  Jan 05 '15 at 20:31
  • Or `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. But it is less readable than other comments. Just to show you were close from your solution :) – β.εηοιτ.βε Jan 05 '15 at 20:33
  • @xam I see. You should have stated from the start you could not use it. I didn't downvote your question but shunning a possible solution because "you are not a big fan" of it likely helped invite the downvotes. – nwk Jan 05 '15 at 20:34
  • sorry, my bad, and I re-worded my questions, make it more clear, and removed the offending words "not a big fan of" for you, would you up-vote me now? thanks anyway – xam Jan 05 '15 at 21:43

2 Answers2

1

Try a function like this

for Saturday:

function LastSaturday($time_string){
     return strtotime('last saturday', strtotime($time_string));
}

or for Sunday:

function LastSunday($time_string){
     return strtotime('last sunday', strtotime($time_string));
}

Basically you just use strtotime('last sunday', strtotime('2011W01')); and replace the sunday with the day you would like.

More Info

Also view CBroe's comment

EDIT

For a better more efficient function use

function LastDay($day, $time_string){
     $day = strtolower($day);
     return strtotime("last $day", strtotime($time_string));
}

Usage:

LastDay('sunday', '2011W01');
Community
  • 1
  • 1
  • Wow i see now, the first part you copied from a comment and the last part which you wrote yourself doesn't work!(' and ") Very well made – Rizier123 Jan 05 '15 at 20:23
  • @Rizier123 Firstly I mentioned the comment, secondly the last part does work and lastly I don't like sarcasm. –  Jan 05 '15 at 20:25
  • 1
    This is CBroe's answer. and this is as far as I got to also. I was hoping to call strtotime once and get what I need, instead of calling it twice. we can do strtotime('2011W011'), but cannot do strtotime('2011W01 minus 1')...Anyway, does anyone has something better then this? – xam Jan 05 '15 at 20:34
  • @xam As far as I know this cannot be done without the `DateTime` class or without this code. Why wouldn't you want to use this, as it works and is the shortest solution? –  Jan 05 '15 at 20:38
  • I did a lot of searching and keep getting the 'DateTime' solution, and it stopped there. what good if the solution cannot be used to some of us, whom do not have DateTime option? For example, @b.enoit.be solution works too. it would not have surfaced if I don't prevent the famous 'DateTime' solutions, this thread would stop as soon as 'DateTime' solution delivered. which would defeated the purpose of this thread. And now, you know not only 'DateTime', but 'strtotime' can do it also, more powers to you and sure it would help many others to come. And thanks for your answers too. – xam Jan 05 '15 at 21:49
1

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.

β.εηοιτ.βε
  • 33,893
  • 13
  • 69
  • 83
  • you nailed it! thanks! I know there has to be a simple way of something like this, but don't know how. Thank you for showing the way. I didn't realized you answered it in the comment, until you posted it as an answer. – xam Jan 05 '15 at 21:23
  • One must be careful on using week numbers with php, because there is a 4 days rule which effects how the week numbers are interpreted, further discussions on that topic: [discussions](http://stackoverflow.com/questions/27809736/php-get-correct-date-by-week-one-of-the-year-using-strtotime) – xam Jan 08 '15 at 23:37