PHP 查找最小数字/日期的快捷方式

发布于 2024-10-28 06:48:14 字数 336 浏览 4 评论 0原文

我遇到的情况是,已创建一系列 15 个日期,当前采用 UNIX 时间戳。

另一个变量

目标是找到 15 个其他日期中 > 大于 $dateidate 的最小日期,并以 'dmY' 格式显示

完成此操作后,有一种方法可以获取其他 15 个日期中第二小的 > 大于 $dateidate 的日期,并以 的格式显示>'dmY'

I have a situation whereby a series of 15 dates have been created, currently in UNIX timestamps.

Another variable <?php $dateidate = date(strtotime('+20 days')); ?>

The objective is to find the smallest of the 15 other dates that is greater > than $dateidate and display in the format of 'd-m-Y'

Once we've done that is there a way to get the second smallest of the 15 other dates that is greater > than $dateidate and display in the format of 'd-m-Y'.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

夜司空 2024-11-04 06:48:15

因此,您有 15 个日期,它们是 UNIX 时间戳。有用。

好的,您可以执行以下操作来轻松完成此操作:

$datearray = array(timestamp1,timestamp2,etc.) // an array of timestamps
$dateidate = time() + 1728000; //current time + 20 days worth of seconds (20 * 24 * 60 * 60)

foreach($datearray as $key => $date)
{
    if($date < $dateidate)
    {
        unset $datearray[$key];  //Remove timestamp from original array if less than $dateidate
    }
}

$earliestdate = min($datearray); 
//min returns the least of the values in the array, opposite of max, which you could use to find the latest date in the array

$date = date('d-m-Y',$earliestdate);

So, you have 15 dates which are UNIX timestamps. Useful.

Ok, here's what you can do to do it easily:

$datearray = array(timestamp1,timestamp2,etc.) // an array of timestamps
$dateidate = time() + 1728000; //current time + 20 days worth of seconds (20 * 24 * 60 * 60)

foreach($datearray as $key => $date)
{
    if($date < $dateidate)
    {
        unset $datearray[$key];  //Remove timestamp from original array if less than $dateidate
    }
}

$earliestdate = min($datearray); 
//min returns the least of the values in the array, opposite of max, which you could use to find the latest date in the array

$date = date('d-m-Y',$earliestdate);
心不设防 2024-11-04 06:48:15

strtotime 生成时间戳。
而不是这样做:

<?php $dateidate = date(strtotime('+20 days')); ?>

这样做:

<?php $dateidate = strtotime('+20 days'); ?>

将所有时间戳放入具有特殊键的数组中,以便您可以区分哪个是您的枢轴。
对该数组进行排序,然后对排序后的数组执行您需要执行的操作。

strtotime generates a timestamp.
instead of this:

<?php $dateidate = date(strtotime('+20 days')); ?>

do this:

<?php $dateidate = strtotime('+20 days'); ?>

Put all timestamps into an array with special keys so you can distinguish which one is your pivot.
Sort that array and do what you need to do with the sorted array.

感情旳空白 2024-11-04 06:48:15

此解决方案过滤 $dates 数组,该数组使用 匿名存储时间戳函数,因此在 $shorterOnes 数组中,您将拥有大于 $dateidate 的所有时间戳。

然后对数组进行排序,第一个将是最小的,依此类推。

$dateidate=strtotime('+20 days');
$dates=array(/*timestamps*/);

$shorterOnes=array_filter($dates, function ($v) use ($dateidate) {
  return $v>$dateidate;
});

sort($shorterOnes);

echo date('d-m-Y', $shorterOnes[0]);
echo date('d-m-Y', $shorterOnes[1]);

匿名函数仅适用于 PHP 5.3。低于此,您需要使用 create_function()

This solution filters the $dates array which stores the timestamps using an anonymous function, so in the $shorterOnes array you will have all the timestamps that are bigger than $dateidate.

Then the array is sorted, the first one will be smallest and so on.

$dateidate=strtotime('+20 days');
$dates=array(/*timestamps*/);

$shorterOnes=array_filter($dates, function ($v) use ($dateidate) {
  return $v>$dateidate;
});

sort($shorterOnes);

echo date('d-m-Y', $shorterOnes[0]);
echo date('d-m-Y', $shorterOnes[1]);

Anonymous functions only work from PHP 5.3. Lower than that, you need to use create_function().

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文