\WP_Stager_Integration\Calendar::calculate_timestamp_week_start(int $year, int $week_number): int

Description

Calculates the start timestamp of a given week (00:00:00 in the current site's timezone, first day of the week).

Takes the site's start_of_week setting into account

Parameters:

ParameterTypeDescription
$year int The year
$week_number int The week number

Returns:

int Unix timestamp for week start (00:00:00)

Information

Fileclass-calendar.php line 688

Full Code

/**
 * Calculates the start timestamp of a given week (00:00:00 in the current site's timezone, first day of the week).
 *
 * Takes the site's <code>start_of_week</code> setting into account
 *
 * @param int $year          The year
 * @param int $week_number   The week number
 *
 * @return int   Unix timestamp for week start (00:00:00)
 */
public static function calculate_timestamp_week_start( $year, $week_number ) {

    // Type casting
    $year        = (int) $year;
    $week_number = (int) $week_number;

    // Get start of week day (0 = Sunday, 1 = Monday, etc)
    $start_of_week = (int) get_option( 'start_of_week', 1 );

    // Local Jan 1 at 00:00
    $datetime = new \DateTimeImmutable( sprintf( '%04d-01-01 00:00:00', $year ), wp_timezone() );

    // Calculate day of week of Jan 1 (Sun=0..Sat=6)
    $day_of_week = (int) $datetime->format( 'w' );

    // Move back to the first chosen week-start on or before Jan 1
    $diff             = ($day_of_week - $start_of_week + 7) % 7;
    $first_week_start = $datetime->modify( "-{$diff} days" );

    // Advance {$week_number - 1} weeks and get timestamp
    $target_timestamp = self::calculate_offset_timestamp( $first_week_start->getTimestamp(), 'P' . max( 0, $week_number - 1 ) . 'W' );

    // Return day start timestamp
    return self::calculate_timestamp_day_start( $target_timestamp );
}

💡 If you ever get stuck or have a question, please check our FAQs, our Free Integration Service, our paid Full Integration Service, or reach out to us!

Get WP Stager Integration

🎁 Limited offer: Use code WELCOME26 to get your first month for free!