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

Weeks are ISO 8601 weeks, running Monday to Sunday, which makes this the exact inverse of {@see \WP_Stager_Integration\Calendar::get_week_number()} and {@see \WP_Stager_Integration\Calendar::get_year_number()}. Every week number in the plugin comes from those two methods, so this has to agree with them: a week number means nothing on its own, only in the numbering scheme it was produced by.

Parameters:

ParameterTypeDescription
$year int The ISO 8601 week-numbering year
$week_number int The ISO 8601 week number (1-53)

Returns:

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

Information

Fileclass-calendar.php line 692

Full Code

/**
 * Calculates the start timestamp of a given week (00:00:00 in the current site's timezone, first day of the week).
 *
 * Weeks are ISO 8601 weeks, running Monday to Sunday, which makes this the exact inverse
 * of {@see \WP_Stager_Integration\Calendar::get_week_number()} and
 * {@see \WP_Stager_Integration\Calendar::get_year_number()}. Every week number in the
 * plugin comes from those two methods, so this has to agree with them: a week number means
 * nothing on its own, only in the numbering scheme it was produced by.
 *
 * @param int $year          The ISO 8601 week-numbering year
 * @param int $week_number   The ISO 8601 week number (1-53)
 *
 * @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;

    // Keep the week number within the range ISO 8601 defines
    $week_number = max( 1, min( 53, $week_number ) );

    // Monday of the requested ISO week, at 00:00 in the site's timezone
    $datetime = new \DateTimeImmutable( 'now', wp_timezone() );
    $datetime = $datetime->setISODate( $year, $week_number, 1 )->setTime( 0, 0, 0 );

    return $datetime->getTimestamp();
}

💡 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!