Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calculate day of month with month, year, day of week and number of week

Tags:

date

php

How can I calculate the day of month in PHP with giving month, year, day of week and number of week.
Like, if I have September 2013 and day of week is Friday and number of week is 2, I should get 6. (9/6/2013 is Friday on the 2nd week.)

like image 716
gskartwii Avatar asked Mar 23 '23 11:03

gskartwii


1 Answers

One way to achieve this is using relative formats for strtotime().

Unfortunately, it's not as straightforward as:

strtotime('Friday of second week of September 2013');

In order for the weeks to work as you mentioned, you need to call strtotime() again with a relative timestamp.

$first_of_month_timestamp = strtotime('first day of September 2013');
$second_week_friday = strtotime('+1 week, Friday', $first_of_month_timestamp);
echo date('Y-m-d', $second_week_friday); // 2013-09-13

Note: Since the first day of the month starts on week one, I've decremented the week accordingly.

like image 82
Jason McCreary Avatar answered Mar 29 '23 22:03

Jason McCreary