WordPress at a glance

get_weekstartend() WP 1.0

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

✈ 1 time = 0.000049s = very fast | 50000 times = 0.33s = very fast PHP 7.1.2RC1, WP 4.7.3

No Hooks.

Return

Array. Keys are 'start' and 'end'.

Usage

get_weekstartend( $mysqlstring, $start_of_week );
$mysqlstring(string) (required)
Date or datetime field type from MySQL.
$start_of_week(int/string)
Start of the week as an integer.
Default: empty string

Code of get_weekstartend: wp-includes/functions.php VER 5.0.1

<?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 = 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' );
}

Related Functions

From tag: date time

No comments
    Hello, !     Log In . Register