get_weekstartend

函数
get_weekstartend ( $mysqlstring, $start_of_week = '' )
参数
  • (string) $mysqlstring Date or datetime field type from MySQL.
    Required:
  • (int|string) $start_of_week Optional. Start of the week as an integer. Default empty string.
    Required:
    Default: (empty)
返回值
  • (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. }
定义位置
相关方法
get_post_parentget_extendedget_last_updatedget_theme_starter_contentget_media_states
引入
0.71
弃用
-

get_weekstartend: 这个函数用来根据当前WordPress的设置来获取一个星期的开始和结束日期。开始和结束日期会以数组形式返回。

从MySQL的日期时间或日期字符串中获取一周的开始和结束时间。

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

常见问题

FAQs
查看更多 >