Skip to content

Commit 90172f5

Browse files
committed
Optimize order date generation
1 parent 323a1a7 commit 90172f5

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
@@ -256,14 +256,15 @@ protected static function get_date_created( $assoc_args ) {
256256
return $current;
257257
}
258258

259-
$dates = array();
260-
$date = strtotime( $start );
261-
while ( $date <= strtotime( $end ) ) {
262-
$dates[] = date( 'Y-m-d', $date );
263-
$date = strtotime( '+1 day', $date );
264-
}
265-
266-
return $dates[ array_rand( $dates ) ];
259+
// Use timestamp-based random selection instead of building date array
260+
// This is much more efficient, especially for large date ranges
261+
$start_timestamp = strtotime( $start );
262+
$end_timestamp = strtotime( $end );
263+
$days_between = (int) ( ( $end_timestamp - $start_timestamp ) / DAY_IN_SECONDS );
264+
265+
// Generate random offset in days and add to start timestamp
266+
$random_days = wp_rand( 0, $days_between );
267+
return date( 'Y-m-d', $start_timestamp + ( $random_days * DAY_IN_SECONDS ) );
267268
}
268269

269270
/**

0 commit comments

Comments
 (0)