$start = '2011-05-12';
$end = '2011-05-30';
$start = strtotime($start);
$end = strtotime($end);
$winningTimes = array();
for ($i = 1; $i <= 3; $i++) {
$winningTimes[] = mt_rand($start, $end);
}
這讓你在給定範圍內的三個時間戳。時間戳僅爲1970年以來的秒數,所以它們是簡單的連續數字。只需使用mt_rand
在起始範圍內選擇一個隨機數。使用例如
date('Y-m-d', $winningTimes[0])
將時間戳轉換回人類可讀的日期。
要確保你得到獨特的天,做這樣的事情:
$start = strtotime('2011-05-12');
$end = strtotime('2011-05-30');
$days = array();
// populate array with all possible days
for ($i = $start; $i <= $end; $i = strtotime('+1 day', $i)) {
$days[] = date('Y-m-d', $i);
}
shuffle($days); // randomize
$days = array_slice($days, 0, 3); // take first 3
只是另一種變體如何可以轉換日期:http://codepad.org/GFUGFnvd – 2011-05-24 02:17:50
@OZ_' strptime'不完全相同,你需要通過'mktime'得到一個時間戳。 'Y-m-d'格式可以由'strtotime'安全地解析,所以這是最簡單的方法。 – deceze 2011-05-24 02:21:25
是的,對,對不起。 – 2011-05-24 02:22:56