Edit: fixed bugs according to comments.
Suppose you have information about start and end dates, let's say the same as getdate () returns, then you can generate the date without having to go through the timestamp:
$year = rand($start_details['year'], $end_details['year']); $isleap = $year % 400 == 0 || ($year % 100 != 0 && $year % 4); $min_yday = $year > $start_details['year'] ? 0 : $start_details['yday']; $max_yday = $year == $end_details['year'] ? $end_details['yday'] : ($isleap ? 365 : 364); $yday = rand($min_yday, $max_yday); $sec_in_day = 24 * 60 * 60; $date_details = getdate($yday * $sec_in_day + ($isleap ? 2 * 365 * $sec_in_day : 0)); return sprintf('%04d-%02d-%02d', $year, $date_details['mon'], $date_details['mday']);
Something like this (I have not tested). The code above assumes UTC, you may need an offset according to your time zone .5
source share