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.
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.
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
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);