$args Args (range|from|to). * @return array{0:string,1:string} */ public static function resolve( array $args ): array { $now = time(); $to = gmdate( 'Y-m-d H:i:s', $now ); if ( ! empty( $args['from'] ) ) { $from = gmdate( 'Y-m-d 00:00:00', (int) strtotime( (string) $args['from'] ) ); if ( ! empty( $args['to'] ) ) { $to = gmdate( 'Y-m-d 23:59:59', (int) strtotime( (string) $args['to'] ) ); } return array( $from, $to ); } switch ( (string) ( $args['range'] ?? '30d' ) ) { case 'today': // Midnight in the store's timezone, expressed in UTC like all // stored order dates. $from = get_gmt_from_date( current_time( 'Y-m-d' ) . ' 00:00:00' ); break; case '7d': $from = gmdate( 'Y-m-d H:i:s', strtotime( '-7 days', $now ) ); break; case '90d': $from = gmdate( 'Y-m-d H:i:s', strtotime( '-90 days', $now ) ); break; case '365d': $from = gmdate( 'Y-m-d H:i:s', strtotime( '-365 days', $now ) ); break; case 'ytd': $from = gmdate( 'Y-01-01 00:00:00', $now ); break; case 'all': $from = '1970-01-01 00:00:00'; break; case '30d': default: $from = gmdate( 'Y-m-d H:i:s', strtotime( '-30 days', $now ) ); break; } return array( $from, $to ); } /** * Choose a sensible time-series bucket (day/week/month) for a span. * * @param string $from UTC lower bound. * @param string $to UTC upper bound. * @return string 'day' | 'week' | 'month' */ public static function bucket_for( string $from, string $to ): string { $days = ( strtotime( $to ) - strtotime( $from ) ) / DAY_IN_SECONDS; if ( $days <= 45 ) { return 'day'; } if ( $days <= 400 ) { return 'week'; } return 'month'; } }