get_weekstartend() WordPress Function

The get_weekstartend() Wordpress function allows you to get the start and end date of the current week. This is useful if you want to display a weekly schedule on your website. The function returns an associative array with two keys: 'start' and 'end'. The 'start' key contains the timestamp of the start of the week, and the 'end' key contains the timestamp of the end of the week.

get_weekstartend( string $mysqlstring, int|string $start_of_week = '' ) #

Get the week start and end from the datetime or date string from MySQL.


Parameters

$mysqlstring

(string)(Required)Date or datetime field type from MySQL.

$start_of_week

(int|string)(Optional) Start of the week as an integer.

Default value: ''


Top ↑

Return

(int[]) Week start and end dates as Unix timestamps.

  • 'start'
    (int) The week start date as a Unix timestamp.
  • 'end'
    (int) The week end date as a Unix timestamp.


Top ↑

Source

File: wp-includes/functions.php

function get_weekstartend( $mysqlstring, $start_of_week = '' ) {
	// MySQL string year.
	$my = substr( $mysqlstring, 0, 4 );

	// MySQL string month.
	$mm = substr( $mysqlstring, 8, 2 );

	// MySQL string day.
	$md = substr( $mysqlstring, 5, 2 );

	// The timestamp for MySQL string day.
	$day = mktime( 0, 0, 0, $md, $mm, $my );

	// The day of the week from the timestamp.
	$weekday = gmdate( 'w', $day );

	if ( ! is_numeric( $start_of_week ) ) {
		$start_of_week = get_option( 'start_of_week' );
	}

	if ( $weekday < $start_of_week ) {
		$weekday += 7;
	}

	// The most recent week start day on or before $day.
	$start = $day - DAY_IN_SECONDS * ( $weekday - $start_of_week );

	// $start + 1 week - 1 second.
	$end = $start + WEEK_IN_SECONDS - 1;
	return compact( 'start', 'end' );
}


Top ↑

Changelog

Changelog
VersionDescription
0.71Introduced.

The content displayed on this page has been created in part by processing WordPress source code files which are made available under the GPLv2 (or a later version) license by theĀ Free Software Foundation. In addition to this, the content includes user-written examples and information. All material is subject to review and curation by the WPPaste.com community.

Show More
Show More