get_weekstartend()
云策文档标注
概述
get_weekstartend() 函数用于从 MySQL 日期或日期时间字符串中获取一周的开始和结束时间,返回 Unix 时间戳数组。它支持自定义一周的起始日,默认使用 WordPress 设置。
关键要点
- 参数 $mysqlstring 是必需的 MySQL 日期或日期时间字符串。
- 参数 $start_of_week 是可选的整数或字符串,指定一周的起始日,默认为空字符串,会使用 get_option('start_of_week') 获取。
- 返回值是一个包含两个整数的数组:start 表示一周开始日期的 Unix 时间戳,end 表示一周结束日期的 Unix 时间戳。
- 函数内部通过 substr() 提取日期部分,使用 mktime() 和 gmdate() 计算时间戳和星期几。
- 相关函数包括 get_option() 和 wp_get_archives(),用于获取选项和显示存档链接。
- 自 WordPress 0.71 版本引入,无后续变更记录。
原文内容
Gets the week start and end from the datetime or date string from MySQL.
Parameters
$mysqlstringstringrequired-
Date or datetime field type from MySQL.
$start_of_weekint|stringoptional-
Start of the week as an integer. Default empty string.
Source
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 = (int) gmdate( 'w', $day );
if ( ! is_numeric( $start_of_week ) ) {
$start_of_week = (int) 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' );
}
Changelog
| Version | Description |
|---|---|
| 0.71 | Introduced. |