Tribe__Date_Utils::get_weekday_timestamp( int $day_of_week, int $week_in_month, int $month, int $year, int $week_direction = 1 )
Gets the timestamp of a day in week, month and year context.
Contents
Kudos to icedwater StackOverflow user in his answer.
Usage examples: "The second Wednesday of March 2015" – get_day_timestamp( 3, 2, 3, 2015, 1) "The last Friday of December 2015" – get_day_timestamp( 5, 1, 12, 2015, -1) "The first Monday of April 2016 – get_day_timestamp( 1, 1, 4, 2016, 1) "The penultimate Thursday of January 2012" – get_day_timestamp( 4, 2, 1, 2012, -1)
Parameters
- $day_of_week
-
(int) (Required) The day representing the number in the week, Monday is
1, Tuesday is2, Sunday is7 - $week_in_month
-
(int) (Required) The week number in the month; first week is
1, second week is2; when direction is reverse then1is last week of the month,2is penultimate week of the month and so on. - $month
-
(int) (Required) The month number in the year, January is
1 - $year
-
(int) (Required) The year number, e.g. "2015"
- $week_direction
-
(int) (Optional) Either
1or-1; the direction for the search referring to the week, defaults to1to specify weeks in natural order so: $week_direction1and $week_in_month1means "first week of the month" $week_direction1and $week_in_month3means "third week of the month" $week_direction-1and $week_in_month1means "last week of the month" $week_direction-1and $week_in_month2means "penultimmate week of the month"Default value: 1
Return
(int) The day timestamp
Source
File: src/Tribe/Date_Utils.php
public static function get_weekday_timestamp( $day_of_week, $week_in_month, $month, $year, $week_direction = 1 ) {
if (
! (
is_numeric( $day_of_week )
&& is_numeric( $week_in_month )
&& is_numeric( $month )
&& is_numeric( $year )
&& is_numeric( $week_direction )
&& in_array( $week_direction, array( - 1, 1 ) )
)
) {
return false;
}
if ( $week_direction > 0 ) {
$startday = 1;
} else {
$startday = date( 't', mktime( 0, 0, 0, $month, 1, $year ) );
}
$start = mktime( 0, 0, 0, $month, $startday, $year );
$weekday = date( 'N', $start );
if ( $week_direction * $day_of_week >= $week_direction * $weekday ) {
$offset = - $week_direction * 7;
} else {
$offset = 0;
}
$offset += $week_direction * ( $week_in_month * 7 ) + ( $day_of_week - $weekday );
return mktime( 0, 0, 0, $month, $startday + $offset, $year );
}