我正在尝试在Laravel中播种DB表。有一个time列,对于该表中的每条记录,我需要使其唯一或至少不相同。
目前,我正在使用这个;它确实给出了一些我正在寻找的结果,但它并不完整。
mt_rand(0,23).":".str_pad(mt_rand(0,59), 2, "0", STR_PAD_LEFT)我的问题是,个位数的时间前面没有0,sec也没有。通常,我计划的是下面的代码,但它一次又一次地玩弄我相同的结果:
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')))),结果总是"05:30:00",所以我不知道下一步该怎么做。
发布于 2020-02-11 02:23:19
您说您正在使用Laravel,那么为什么不直接使用DateTime generation的内置Faker库呢
$faker = Faker::create();
$faker->time('H:i')以下是文档中与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'发布于 2020-02-11 02:24:50
考虑到你正在使用Laravel,@leek的答案可能更好,但更通用的获取所需内容的方法如下:
$dt = new DateTime();
var_dump($dt->format('H:i:s'));但是,如果您每秒运行该脚本超过一次,则这将不够唯一。当然,如果运行超过1天,它将(潜在地)不是唯一的。
https://stackoverflow.com/questions/60156185
复制相似问题