Using WordPress ‘get_weekstartend()’ PHP function

The get_weekstartend() WordPress PHP function retrieves the start and end dates of a week from a MySQL datetime or date string.

Usage

get_weekstartend( $mysqlstring, $start_of_week );

Example:

$week_info = get_weekstartend( '2023-05-06', 1 );
echo 'Week starts on: ' . $week_info['start'] . ', and ends on: ' . $week_info['end'];

Output:

Week starts on: 2023-05-01, and ends on: 2023-05-07

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: ''.

More information

See WordPress Developer Resources: get_weekstartend()

Examples

Get the start and end dates of the current week

This example retrieves the start and end dates of the current week, with the week starting on Monday.

$current_date = date( 'Y-m-d' );
$week_info = get_weekstartend( $current_date, 1 );
echo 'Week starts on: ' . $week_info['start'] . ', and ends on: ' . $week_info['end'];

Get the start and end dates of the week for a specific date

This example retrieves the start and end dates of the week for a given date, with the week starting on Sunday.

$given_date = '2023-05-06';
$week_info = get_weekstartend( $given_date, 0 );
echo 'Week starts on: ' . $week_info['start'] . ', and ends on: ' . $week_info['end'];

Get the start and end dates of the week using a custom week start day

This example retrieves the start and end dates of the week for a given date, with the week starting on Wednesday.

$given_date = '2023-05-06';
$week_info = get_weekstartend( $given_date, 3 );
echo 'Week starts on: ' . $week_info['start'] . ', and ends on: ' . $week_info['end'];

Get the start and end dates of the week from a MySQL datetime string

This example retrieves the start and end dates of the week from a MySQL datetime string, with the week starting on Monday.

$mysql_datetime = '2023-05-06 15:30:00';
$week_info = get_weekstartend( $mysql_datetime, 1 );
echo 'Week starts on: ' . $week_info['start'] . ', and ends on: ' . $week_info['end'];

Get the start and end dates of the week without specifying the start day

This example retrieves the start and end dates of the week for a given date, without specifying the week start day. It uses the default week start day set in the WordPress settings.

$given_date = '2023-05-06';
$week_info = get_weekstartend( $given_date );
echo 'Week starts on: ' . $week_info['start'] . ', and ends on: ' . $week_info['end'];