0

I would like to extract beginning DateTime of last weekend of certain month (i.e. March) in current year, using PHP?

Example: 

Last weekend of March this year would be: 23-03-2018.

 

Lululu
  • 675
  • 3
  • 9
  • 21
  • 2
    When you say "Last weekend" are you talking about Saturday or Sunday? You could say strtotime("last saturday of march 2017") and it'll give you your answer. – ChrisG Jan 12 '18 at 11:17
  • 1
    Welcome to Stack Overflow! Please take the tour, have a look around, and read through the help center, in particular How do I ask a good question? and What topics can I ask about here?. From that second link: "Questions asking for homework help must include a summary of the work you've done so far to solve the problem, and a description of the difficulty you are having solving it." – Mittal Patel Jan 12 '18 at 11:26
  • Possible duplicate of [How to get the current date and time in PHP?](https://stackoverflow.com/questions/470617/how-to-get-the-current-date-and-time-in-php) – Milad Rashidi Jan 12 '18 at 11:30
  • @ChrisG Amazing! Didn't know for that trick, I've solved problem. Please post answer so I can mark it. – Lululu Jan 12 '18 at 11:33
  • @Lululu but that won't work in all cases. If I understand your question case you want a "full" weekend of the month. As Chris suggests will give output 2018-03-31 which is not the last full weekend. – Andreas Jan 12 '18 at 12:00
  • @Lululu have you looked at my answer? – Andreas Jan 12 '18 at 13:34
  • @Andreas Yes, thanks for your response, but Chris did previously answered it and I succeeded using it. Thanks again for response. – Lululu Jan 12 '18 at 13:39
  • @Lululu But chris solution returns incorrect output: https://3v4l.org/hjBhO – Andreas Jan 12 '18 at 13:43
  • @Andreas My bad. Thanks for answer! – Lululu Jan 12 '18 at 14:52

2 Answers2

0

To get the last full weekend (I read the question as that is what you want) you need to find last date of month and look at what day that is.
I use date("w") to get the date number 0 for Sunday and 6 for Saturday.
If the number is 5 or larger a full weekend can't happen that month.

If that is the case I remove a few days from the Unix time and find the previous Friday.

$month = "March";
If(date("w", strtotime("last day of ". $month)) >= 5){
    $unix = strtotime("last day of ". $month)-86400*6;
    Echo date("Y-m-d", strtotime("previous Friday " .date("Y-m-d", $unix)));
}Else{
    Echo date("Y-m-d", strtotime("previous Friday " .date("Y-m-d",strtotime("last day of ". $month))));
}

Try it here: https://3v4l.org/O4TDd

Andreas
  • 23,610
  • 6
  • 30
  • 62
-2

I think you should find a solution with strtotime

You can iterate months with

strtotime("-1 months"));
strtotime("1 months"));

and then get the last week end days dates with

strtotime("last saturday");
strtotime("last sunday");

Try :

echo strtotime("+[your variable]"+ months last sunday");

Where [your variable] is the number of months before or after current month

Edit :

after seeing @ChrisG answer in the comments, the best solution is in fact :

 $month = [your month variable];
 echo strtotime("last saturday of "+ $month);