WordPress at a glance
function is not described

Automattic\WooCommerce\Admin\API\Reports

DataStore::update_interval_boundary_dates() protected WC 1.0

Updates start and end dates for intervals so that they represent intervals' borders, not times when data in db were recorded.

E.g. if there are db records for only Tuesday and Thursday this week, the actual week interval is [Mon, Sun], not [Tue, Thu].

{} It's a method of the class: DataStore{}

No Hooks.

Return

null. Nothing.

Usage

// protected - for code of main (parent) or child class
$result = $this->update_interval_boundary_dates( $start_datetime, $end_datetime, $time_interval, $intervals );
$start_datetime(DateTime) (required) (passed by reference — &)
Start date.
$end_datetime(DateTime) (required) (passed by reference — &)
End date.
$time_interval(string) (required) (passed by reference — &)
Time interval, e.g. day, week, month.
$intervals(array) (required) (passed by reference — &)
Array of intervals extracted from SQL db.

Code of DataStore::update_interval_boundary_dates() WC 5.2.2

<?php
protected function update_interval_boundary_dates( $start_datetime, $end_datetime, $time_interval, &$intervals ) {
	$local_tz = new \DateTimeZone( wc_timezone_string() );
	foreach ( $intervals as $key => $interval ) {
		$datetime = new \DateTime( $interval['datetime_anchor'], $local_tz );

		$prev_start = TimeInterval::iterate( $datetime, $time_interval, true );
		// @todo Not sure if the +1/-1 here are correct, especially as they are applied before the ?: below.
		$prev_start_timestamp = (int) $prev_start->format( 'U' ) + 1;
		$prev_start->setTimestamp( $prev_start_timestamp );
		if ( $start_datetime ) {
			$date_start                      = $prev_start < $start_datetime ? $start_datetime : $prev_start;
			$intervals[ $key ]['date_start'] = $date_start->format( 'Y-m-d H:i:s' );
		} else {
			$intervals[ $key ]['date_start'] = $prev_start->format( 'Y-m-d H:i:s' );
		}

		$next_end           = TimeInterval::iterate( $datetime, $time_interval );
		$next_end_timestamp = (int) $next_end->format( 'U' ) - 1;
		$next_end->setTimestamp( $next_end_timestamp );
		if ( $end_datetime ) {
			$date_end                      = $next_end > $end_datetime ? $end_datetime : $next_end;
			$intervals[ $key ]['date_end'] = $date_end->format( 'Y-m-d H:i:s' );
		} else {
			$intervals[ $key ]['date_end'] = $next_end->format( 'Y-m-d H:i:s' );
		}

		$intervals[ $key ]['interval'] = $time_interval;
	}
}