PHP。在数组中随机生成的日期中查找最低日期

PHP。在数组中随机生成的日期中查找最低日期,php,arrays,date,max,min,Php,Arrays,Date,Max,Min,我刚开始学习PHP。在过去的三天里,我因一项任务而受苦。谷歌拒绝提供帮助。问题随之而来 有一项任务-在数组$date中随机生成的日期中查找最低日期。我就是这么做的: // Create two variables of type timestamp with the minimum and maximum value dates     $Min = strtotime('1970-01-01 00:00:00'); $Max = strtotime('3000-01-01 00:00

我刚开始学习PHP。在过去的三天里,我因一项任务而受苦。谷歌拒绝提供帮助。问题随之而来

有一项任务-在数组$date中随机生成的日期中查找最低日期。我就是这么做的:

// Create two variables of type timestamp with the minimum and maximum value dates

    $Min = strtotime('1970-01-01 00:00:00');
    $Max = strtotime('3000-01-01 00:00:00');

// Create an array of 5 random dates, using a random number generator

    for ($i = 0; $i<5; $i++) {$date[$i] = rand($min, $max);}

// Check, like all good

    var_dump($date);

// Now I want to select the day of the accident the smallest output of dates. Or, for example, the largest month. I enter the command

    $Y = min(date('d', $date[$i]));

    var_dump($y);
宾果游戏!!!一切都成功了但是如果数组$date中有25000个元素,我要写这行多长时间


亲爱的程序员,请帮忙。如何合理、简洁、清晰地写最后一行,在没有错误的情况下执行任务?怎么了?谢谢大家!

稳健的解决方案是使用for循环来比较每个元素,可能是foreach循环的每个元素

$min = date('d', $date[0]);
foreach($date as $d){
    $min = min(date('d', $d), $min);
}

for循环也会使用相同的技巧,但您必须引用索引号。除非你也想知道它是数组的哪个元素,否则就省去麻烦吧。

我知道这看起来很奇怪,但你可以使用little php(7)提供的:

// 10 random ints
$randoms = array_map(function(){ 
    return rand(); 
},range(0,10));

// 10 random dates
$dates = array_map(function($rand_int){ 
    // there is probably a easier way to do that
    return (new DateTimeImmutable())->setTimeStamp($rand_int); 
},$randoms);

// apply min and extract Day or whatever you like.
var_dump( min($dates)->format('Y-m-d - l') );

// result:
// string(21) "1974-11-23 - Saturday"
下面这一点很重要:

从PHP5.2.2开始,可以使用比较运算符比较DateTime对象


哪一个
min
应该在引擎盖下执行。

您几乎可以正常工作,只是一些小错误

  • 未使用的变量($Min和$Max)
  • 未定义的变量($date、$min和$max)
  • 未定义的键($date[$i])
  • min()的用法不正确
在php中,通常将变量名写成小写,函数名也是如此

// define all variables needed
$dates = array();
$min = strtotime('1970-01-01 00:00:00');
$max = strtotime('3000-01-01 00:00:00');

for ($i = 0; $i<5; $i++) {
    $dates[$i] = rand($min, $max);
}

// lookup the lowest date int and convert it to a date string
$accident = date('d', min($dates));
//定义所需的所有变量
$dates=array();
$min=strottime('1970-01-01 00:00:00');
$max=strottime('3000-01-01 00:00:00');

对于($i=0;$i您可以使用
natsort
按从低到高的顺序对时间戳进行排序,然后从排序后的数组中选择第一个元素

natsort($dates);
$lowest = $dates[0];
这样,您应该拥有时间戳中第一个可能的最低值

这是上的手册页,了解有关该功能的更多信息

我的朋友

在使用函数之前,您应该阅读有关函数的文档

因此,您不能像这样使用strotime:

strtotime('3000-01-01 00:00:00');
它将返回“false”而不是整数。请小心。

还有一点:

您还应该知道,在php中,变量名是区分大小写的

 $Min = strtotime('1970-01-01 00:00:00');
    $Max = strtotime('3000-01-01 00:00:00');

// Create an array of 5 random dates, using a random number generator

    for ($i = 0; $i<5; $i++) {$date[$i] = rand($min, $max);}
$Min=strotime('1970-01-01 00:00:00');
$Max=strottime('3000-01-01 00:00:00');
//使用随机数生成器创建5个随机日期的数组

对于($i=0;$ihint:使用
var\u dump
进一步调查提示:您正在使用一个int数组,并试图获取其最小值(但您没有将int数组传递给min(),而是传递了一个int).Hint2:即使没有该选项,也必须有一种方法在数组上循环并将当前元素与某个元素进行比较。
date
返回一个字符串,
min
需要一个数组。
min(date(…)
永远不会起作用。执行
min()
对单个值的处理无论如何都是毫无意义的。单个值始终同时是其最小值和最大值。
min(数组(date(),date(),date(),date(),…)
会起作用。(当然,在现实场景中,您会将日期加载到数据库中(因为您可能正在对它们执行其他操作),对其进行排序,然后获取第一个。)您正在使用大写字母定义$Min和$Max(不常见)。这些变量未在代码中使用,但您有未设置的类似变量$Min和$Max。
 $Min = strtotime('1970-01-01 00:00:00');
    $Max = strtotime('3000-01-01 00:00:00');

// Create an array of 5 random dates, using a random number generator

    for ($i = 0; $i<5; $i++) {$date[$i] = rand($min, $max);}
<?php
// Create two variables of type timestamp with the minimum and maximum value dates

$Min = strtotime('1970-01-01 00:00:00');
var_dump('Min = '. $Min);
$Max = strtotime('2030-01-01 00:00:00');
var_dump('Max = '. $Max);


// Create an array of 5 random dates, using a random number generator

for ($i = 0; $i<5; $i++) {$date[$i] = rand($Min, $Max);}

// Check, like all good

var_dump($date);

// Now I want to select the day of the accident the smallest output of dates. Or, for example, the largest month. I enter the command

$array = [];
foreach($date as $value)
{
    $array[date('Y-m-d', $value)] = date('d', $value);
}
var_dump($array);


$Y = min($array);

var_dump($Y);