我想寫一個PHP腳本(或一行代碼)來回顯兩個日期之間的隨機時間和日期,例如2日期值之間的隨機時間和日期
2012-12-24 13:03
這將是第一次我選擇的日期2012年10月1日和任何想法之間一月2013
如何最好地做到這一點?提前致謝。
我想寫一個PHP腳本(或一行代碼)來回顯兩個日期之間的隨機時間和日期,例如2日期值之間的隨機時間和日期
2012-12-24 13:03
這將是第一次我選擇的日期2012年10月1日和任何想法之間一月2013
如何最好地做到這一點?提前致謝。
容易:)只要選擇2個隨機日期,轉換爲時代,而這些2個值:)之間的隨機
EPOCH - 一直以來1/1/1970,單位爲秒。
您可以使用strtotime()
函數使日期字符串變爲紀元時間,並使用date()
函數使其成爲另一種方式。
function rand_date($min_date, $max_date) {
/* Gets 2 dates as string, earlier and later date.
Returns date in between them.
*/
$min_epoch = strtotime($min_date);
$max_epoch = strtotime($max_date);
$rand_epoch = rand($min_epoch, $max_epoch);
return date('Y-m-d H:i:s', $rand_epoch);
}
好了,這裏的東西
$date_start = strtotime('1 October 2012');
$date_end = strtotime('1 January 2013');
$rand_date = rand($date_start, $date_end);
echo(date('d.m.Y H:i', $rand_date));
假設你要包括10月1日,但不包括1月1日......
$start = strtotime("2012-10-01 00:00:00");
$end = strtotime("2012-12-31 23:59:59");
$randomDate = date("Y-m-d H:i:s", rand($start, $end));
echo $randomDate;
那麼瘋狂,它只是可以工作
function randomDate($start_date, $end_date)
{
//make timetamps
$min = strtotime($start_date);
$max = strtotime($end_date);
//random date
$rand_date = rand($min, $max);
//format it
return date('Y-m-d H:i:s', $rand_date);
}
下面是一些代碼來完成th是:
$randDate=date('Y-m-d', mt_rand(strtotime('2012-10-01'), strtotime('2013-01-01')));
你可能要定義的分辨率,例如一分鐘,三分鐘或15秒或一個半一天或什麼不是。隨機性應用於整個時期,我選擇了一分鐘作爲示範目的(在您的時期內有132480分鐘)。
$start = new Datetime('1st October 2012');
$end = new Datetime('1st Jan 2013');
$interval = new DateInterval('PT1M'); // Resolution: 1 Minute
$period = new DatePeriod($start, $interval, $end);
$random = new RandomIterator($period);
list($result) = iterator_to_array($random, false) ? : [null];
例如,這給:
class DateTime#7 (3) {
public $date =>
string(19) "2012-10-16 02:06:00"
public $timezone_type =>
int(3)
public $timezone =>
string(13) "Europe/Berlin"
}
你可以find the RandomIterator
here。沒有它,它會使用稍長(約1.5比上面的例子迭代次數):
$count = iterator_count($period);
$random = rand(1, $count);
$limited = new LimitIterator(new IteratorIterator($period), $random - 1, 1);
$limited->rewind();
$result = $limited->current();
我也試圖與秒,但是這將需要相當長。你可能首先想要找到一個隨機的一天(92天),然後隨機選擇一個時間。
而且我已經運行一些測試,我無法找到使用DatePeriod
到目前爲止,只要你像秒鐘常見的解決任何好處:
$start = new Datetime('1st October 2012');
$end = new Datetime('1st Jan 2013');
$random = new DateTime('@' . mt_rand($start->getTimestamp(), $end->getTimestamp()));
或分:
/**
* @param DateTime $start
* @param DateTime $end
* @param int|DateInterval $resolution in Seconds or as DateInterval
* @return DateTime
*/
$randomTime = function (DateTime $start, DateTime $end, $resolution = 1) {
if ($resolution instanceof DateInterval) {
$interval = $resolution;
$resolution = ($interval->m * 2.62974e6 + $interval->d) * 86400 + $interval->h * 60 + $interval->s;
}
$startValue = floor($start->getTimestamp()/$resolution);
$endValue = ceil($end->getTimestamp()/$resolution);
$random = mt_rand($startValue, $endValue) * $resolution;
return new DateTime('@' . $random);
};
$random = $randomTime($start, $end, 60);
這很簡單 - 你需要一個隨機的時間戳。然後從隨機時間戳,你將能夠提取時間... – Yang
你有沒有嘗試過什麼? –
你想在隨機日期中包括10月1日和1月1日,還是隻在它們之間? – Ray