简体   繁体   English

通过在php中从当前时间添加或减去它来随机化时间

[英]Randomize timing by either adding or subtracting it from current time in php

I am trying to seed DB table in Laravel.我正在尝试在 Laravel 中播种 DB 表。 There is a time column which I need to have unique or at least not same for every record in that table.有一个time列,我需要对该表中的每条记录都具有唯一性或至少不相同。

Currently, I am using this;目前,我正在使用它; which does give the result to somewhat I am looking for but it's not complete.这确实给了我正在寻找的结果,但它并不完整。

mt_rand(0,23).":".str_pad(mt_rand(0,59), 2, "0", STR_PAD_LEFT)

My issue is that the single digit time don't have a 0 in front and sec are missing.我的问题是单位数时间前面没有 0 并且缺少sec Normally, what I was planning is the below code but it game me same results over and over:通常,我计划的是下面的代码,但它一遍又一遍地给我带来相同的结果:

date('H:i:s', strtotime( ((srand(0,1) ? '-'.mt_rand(1,24) : '+'.mt_rand(1,24).' '.rand(0,1) ? 'minute' : 'hour')), strtotime(date('H:i:s')))),

Result is "05:30:00" always so I am confused as to what to do next.结果总是"05:30:00"所以我对接下来要做什么感到困惑。

You said you are using Laravel, so why not just use the built-in Faker library for DateTime generation ?你说你正在使用 Laravel,那么为什么不使用内置的 Faker 库来生成 DateTime呢?

$faker = Faker::create();
$faker->time('H:i')

From the documentation, here is the available DateTime related outputs:从文档中,这里是可用的 DateTime 相关输出:

unixTime($max = 'now')                // 58781813
dateTime($max = 'now', $timezone = null) // DateTime('2008-04-25 08:37:17', 'UTC')
dateTimeAD($max = 'now', $timezone = null) // DateTime('1800-04-29 20:38:49', 'Europe/Paris')
iso8601($max = 'now')                 // '1978-12-09T10:10:29+0000'
date($format = 'Y-m-d', $max = 'now') // '1979-06-09'
time($format = 'H:i:s', $max = 'now') // '20:49:42'
dateTimeBetween($startDate = '-30 years', $endDate = 'now', $timezone = null) // DateTime('2003-03-15 02:00:49', 'Africa/Lagos')
dateTimeInInterval($startDate = '-30 years', $interval = '+ 5 days', $timezone = null) // DateTime('2003-03-15 02:00:49', 'Antartica/Vostok')
dateTimeThisCentury($max = 'now', $timezone = null)     // DateTime('1915-05-30 19:28:21', 'UTC')
dateTimeThisDecade($max = 'now', $timezone = null)      // DateTime('2007-05-29 22:30:48', 'Europe/Paris')
dateTimeThisYear($max = 'now', $timezone = null)        // DateTime('2011-02-27 20:52:14', 'Africa/Lagos')
dateTimeThisMonth($max = 'now', $timezone = null)       // DateTime('2011-10-23 13:46:23', 'Antarctica/Vostok')
amPm($max = 'now')                    // 'pm'
dayOfMonth($max = 'now')              // '04'
dayOfWeek($max = 'now')               // 'Friday'
month($max = 'now')                   // '06'
monthName($max = 'now')               // 'January'
year($max = 'now')                    // '1993'
century                               // 'VI'
timezone                              // 'Europe/Paris'

While @leek's answer is probably better considering you're using Laravel, a more generic way of getting what you need is the following:虽然考虑到您使用的是 Laravel,@leek 的答案可能更好,但获得所需内容的更通用方法如下:

$dt = new DateTime();
var_dump($dt->format('H:i:s'));

However, this will not be unique enough if you're running the script more than once a second.但是,如果您每秒多次运行脚本,这将不够独特。 And of course, it will (potentially) not be unique if you run it over more than 1 day.当然,如果运行超过 1 天,它(可能)不会是唯一的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM