get_weekstartend

The timeline below displays how wordpress function get_weekstartend has changed across different WordPress versions. If a version is not listed, refer to the next available version below.

WordPress Version: 6.1

/**
 * Gets the week start and end from the datetime or date string from MySQL.
 *
 * @since 0.71
 *
 * @param string     $mysqlstring   Date or datetime field type from MySQL.
 * @param int|string $start_of_week Optional. Start of the week as an integer. Default empty string.
 * @return int[] {
 *     Week start and end dates as Unix timestamps.
 *
 *     @type int $start The week start date as a Unix timestamp.
 *     @type int $end   The week end date as a Unix timestamp.
 * }
 */
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');
}

WordPress Version: 5.9

/**
 * Get the week start and end from the datetime or date string from MySQL.
 *
 * @since 0.71
 *
 * @param string     $mysqlstring   Date or datetime field type from MySQL.
 * @param int|string $start_of_week Optional. Start of the week as an integer. Default empty string.
 * @return int[] {
 *     Week start and end dates as Unix timestamps.
 *
 *     @type int $start The week start date as a Unix timestamp.
 *     @type int $end   The week end date as a Unix timestamp.
 * }
 */
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');
}

WordPress Version: 5.3

/**
 * Get the week start and end from the datetime or date string from MySQL.
 *
 * @since 0.71
 *
 * @param string     $mysqlstring   Date or datetime field type from MySQL.
 * @param int|string $start_of_week Optional. Start of the week as an integer. Default empty string.
 * @return array Keys are 'start' and 'end'.
 */
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');
}

WordPress Version: 4.4

/**
 * Get the week start and end from the datetime or date string from MySQL.
 *
 * @since 0.71
 *
 * @param string     $mysqlstring   Date or datetime field type from MySQL.
 * @param int|string $start_of_week Optional. Start of the week as an integer. Default empty string.
 * @return array Keys are 'start' and 'end'.
 */
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 = date('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');
}

WordPress Version: 4.0

/**
 * Get the week start and end from the datetime or date string from MySQL.
 *
 * @since 0.71
 *
 * @param string     $mysqlstring   Date or datetime field type from MySQL.
 * @param int|string $start_of_week Optional. Start of the week as an integer. Default empty string.
 * @return array Keys are 'start' and 'end'.
 */
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 = date('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 + 7 days - 1 second.
    $end = $start + 7 * DAY_IN_SECONDS - 1;
    return compact('start', 'end');
}

WordPress Version: 3.7

/**
 * Get the week start and end from the datetime or date string from mysql.
 *
 * @since 0.71
 *
 * @param string $mysqlstring Date or datetime field type from mysql.
 * @param int $start_of_week Optional. Start of the week as an integer.
 * @return array Keys are 'start' and 'end'.
 */
function get_weekstartend($mysqlstring, $start_of_week = '')
{
    $my = substr($mysqlstring, 0, 4);
    // Mysql string Year
    $mm = substr($mysqlstring, 8, 2);
    // Mysql string Month
    $md = substr($mysqlstring, 5, 2);
    // Mysql string day
    $day = mktime(0, 0, 0, $md, $mm, $my);
    // The timestamp for mysqlstring day.
    $weekday = date('w', $day);
    // The day of the week from the timestamp
    if (!is_numeric($start_of_week)) {
        $start_of_week = get_option('start_of_week');
    }
    if ($weekday < $start_of_week) {
        $weekday += 7;
    }
    $start = $day - DAY_IN_SECONDS * ($weekday - $start_of_week);
    // The most recent week start day on or before $day
    $end = $start + 7 * DAY_IN_SECONDS - 1;
    // $start + 7 days - 1 second
    return compact('start', 'end');
}