Automattic\WooCommerce\Admin\API\Reports
TimeInterval::get_timeframe_dates
Get dates from a timeframe string.
The period is selected from the calendar date of $current_date in its own timezone, so the argument should be anchored to the site timezone: the returned strings are naive datetimes that downstream consumers parse as site-local. When omitted, it defaults to the current time in the site timezone. Note that the passed object is modified in place by the date calculations.
Метод класса: TimeInterval{}
Хуков нет.
Возвращает
array|false. Array of start and end dates, or false for an unknown timeframe.
Использование
$result = TimeInterval::get_timeframe_dates( $timeframe, $current_date );
- $timeframe(строка) (обязательный)
- Timeframe to use. One of: last_week|last_month|last_quarter|last_6_months|last_year.
- $current_date(DateTime|null)
- DateTime of current date to compare.
По умолчанию:null
Код TimeInterval::get_timeframe_dates() TimeInterval::get timeframe dates WC 11.2.2
public static function get_timeframe_dates( $timeframe, $current_date = null ) {
if ( ! $current_date ) {
// The returned date strings are interpreted in the site timezone (see DataStore::normalize_timezones()),
// so the reference "now" must use the site timezone too, not the PHP default (UTC in WordPress).
$current_date = new \DateTime( 'now', wp_timezone() );
}
$current_year = $current_date->format( 'Y' );
$current_month = $current_date->format( 'm' );
if ( 'last_week' === $timeframe ) {
return array(
'start' => $current_date->modify( 'last week monday' )->format( 'Y-m-d 00:00:00' ),
'end' => $current_date->modify( 'this sunday' )->format( 'Y-m-d 23:59:59' ),
);
}
if ( 'last_month' === $timeframe ) {
return array(
'start' => $current_date->modify( 'first day of previous month' )->format( 'Y-m-d 00:00:00' ),
'end' => $current_date->modify( 'last day of this month' )->format( 'Y-m-d 23:59:59' ),
);
}
if ( 'last_quarter' === $timeframe ) {
switch ( $current_month ) {
case $current_month >= 1 && $current_month <= 3:
return array(
'start' => ( $current_year - 1 ) . '-10-01 00:00:00',
'end' => ( $current_year - 1 ) . '-12-31 23:59:59',
);
case $current_month >= 4 && $current_month <= 6:
return array(
'start' => $current_year . '-01-01 00:00:00',
'end' => $current_year . '-03-31 23:59:59',
);
case $current_month >= 7 && $current_month <= 9:
return array(
'start' => $current_year . '-04-01 00:00:00',
'end' => $current_year . '-06-30 23:59:59',
);
case $current_month >= 10 && $current_month <= 12:
return array(
'start' => $current_year . '-07-01 00:00:00',
'end' => $current_year . '-09-30 23:59:59',
);
}
}
if ( 'last_6_months' === $timeframe ) {
if ( $current_month >= 1 && $current_month <= 6 ) {
return array(
'start' => ( $current_year - 1 ) . '-07-01 00:00:00',
'end' => ( $current_year - 1 ) . '-12-31 23:59:59',
);
}
return array(
'start' => $current_year . '-01-01 00:00:00',
'end' => $current_year . '-06-30 23:59:59',
);
}
if ( 'last_year' === $timeframe ) {
return array(
'start' => ( $current_year - 1 ) . '-01-01 00:00:00',
'end' => ( $current_year - 1 ) . '-12-31 23:59:59',
);
}
return false;
}