Skip to content

Commit 0bb12f3

Browse files
committed
Optimize date generation for batch orders
1 parent 5332818 commit 0bb12f3

File tree

1 file changed

+9
-8
lines changed

1 file changed

+9
-8
lines changed

includes/Generator/Order.php

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -289,14 +289,15 @@ protected static function get_date_created( $assoc_args ) {
289289
return $current;
290290
}
291291

292-
$dates = array();
293-
$date = strtotime( $start );
294-
while ( $date <= strtotime( $end ) ) {
295-
$dates[] = date( 'Y-m-d', $date );
296-
$date = strtotime( '+1 day', $date );
297-
}
298-
299-
return $dates[ array_rand( $dates ) ];
292+
// Use timestamp-based random selection instead of building date array
293+
// This is much more efficient, especially for large date ranges
294+
$start_timestamp = strtotime( $start );
295+
$end_timestamp = strtotime( $end );
296+
$days_between = (int) ( ( $end_timestamp - $start_timestamp ) / DAY_IN_SECONDS );
297+
298+
// Generate random offset in days and add to start timestamp
299+
$random_days = wp_rand( 0, $days_between );
300+
return date( 'Y-m-d', $start_timestamp + ( $random_days * DAY_IN_SECONDS ) );
300301
}
301302

302303
/**

0 commit comments

Comments
 (0)