【问题标题】:Random time and date between 2 date values2 个日期值之间的随机时间和日期
【发布时间】:2023-03-11 21:28:01
【问题描述】:

我正在尝试编写一个 php 脚本(或代码行)来回显 2 个日期之间的随机时间和日期,例如

2012-12-24 13:03

这将在我选择的 2012 年 10 月 1 日和 2013 年 1 月 1 日之间。

任何想法如何最好地做到这一点?提前致谢。

【问题讨论】:

  • 这很简单——你需要一个随机的时间戳。然后从随机时间戳中您将能够提取时间...
  • 你有没有尝试过?
  • 您想在随机日期中包含 10 月 1 日和 1 月 1 日还是仅在它们之间?

标签: php


【解决方案1】:

简单 :) 只需选择 2 个随机日期,转换为 EPOCH,并在这 2 个值之间随机 :)

EPOCH - 自 1970 年 1 月 1 日以来的时间,以秒为单位。
您可以使用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);
}

【讨论】:

  • 这应该是一条评论。答案应该有更多的“肉”
  • @Infinity 这就是我最终做到的方式,感谢所有答案。
  • 大声笑!为什么我一开始没有想到。惊人的! +1
  • 当我希望它持续两周时,我只能让它回到七秒。为什么会发生这种情况?
【解决方案2】:

您可能想要定义一个分辨率,例如一分钟、三分钟或 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);

【讨论】:

  • + 在 TraversableIteratorIterator + LimitIterator 上创建自己的 seek
  • @Baba:是的,但这会重复两次。也许您知道一些 kewl 算法,可以从一组未知元素中获取随机值,而无需预先知道集合的大小。
  • 没有loop 就不可能 可以做的是通过扩展DatePeriod 来实现ArrayIterator 将循环限制为只有一个,这样您就可以消除重复循环并只使用@987654337 @ 和 ArrayIterator::seek 获取 rand 日期
  • @Baba 然后我有所有 DateTime 对象的完整副本(检查:CachingIterator 也设置了完整的缓存标志)。不是有一些巧妙的数学公式吗?像只存储集合中所有元素的一半还是有点像?
  • 它们可能是我可以实现的算法,但它们都需要长度......但我认为我即将找到一种明确的方法来解决上述代码而无需多个循环......一会儿
【解决方案3】:

假设您想包括 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;

【讨论】:

    【解决方案4】:

    太疯狂了,它可能会起作用

    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);
    }
    

    【讨论】:

      【解决方案5】:

      这里有一些代码来完成这个:

      $randDate=date('Y-m-d', mt_rand(strtotime('2012-10-01'), strtotime('2013-01-01')));
      

      【讨论】:

        【解决方案6】:

        好的,这里有东西

        $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));
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2015-10-01
          • 2021-07-30
          • 2017-06-03
          • 2015-03-31
          • 2014-05-22
          • 1970-01-01
          • 2013-03-01
          • 1970-01-01
          相关资源
          最近更新 更多