如何在PHP中找到最近的星期几?

发布于 2024-08-31 18:44:45 字数 108 浏览 3 评论 0原文

如果最初我有一个像这样的日期字符串:07.05.2010,如何在 PHP 中找到一周中最近的特定日期?例如,我想找到最近的星期日(或一周中的任何一天)。我怎样才能实现这个?谢谢

How to find a speciefic nearest day of the week in PHP if initially I have a date string like: 07.05.2010? For example, I want to find the nearest Sunday (or any day of the week). How can I implement this? Thanks

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

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

发布评论

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

评论(7

千里故人稀 2024-09-07 18:44:45

以防万一您想要最近一天而不是下一天,这里有一种方法可以做到这一点。

$target = "Sunday";
$date   = "07.05.2010";

// Old-school DateTime::createFromFormat
list($dom, $mon, $year) = sscanf($date, "%02d.%02d.%04d");
$date = new DateTime("$year/$mon/$dom -4 days");
// Skip ahead to $target day
$date->modify("next $target");

echo $date->format("d.m.Y");

从 PHP 5.3 开始,中间部分可以简单地表示为

$date = DateTime::createFromFormat("!d.m.Y", $date)
        ->modify("-4 days")->modify("next $target");

Just in case you wanted the nearest day rather than the next one, here is a way to do that.

$target = "Sunday";
$date   = "07.05.2010";

// Old-school DateTime::createFromFormat
list($dom, $mon, $year) = sscanf($date, "%02d.%02d.%04d");
$date = new DateTime("$year/$mon/$dom -4 days");
// Skip ahead to $target day
$date->modify("next $target");

echo $date->format("d.m.Y");

And as of PHP 5.3, that middle portion can be simply

$date = DateTime::createFromFormat("!d.m.Y", $date)
        ->modify("-4 days")->modify("next $target");
酒中人 2024-09-07 18:44:45

这应该做:

echo date('d.m.Y', strtotime('next Sunday', strtotime('07.05.2010')));

This should do:

echo date('d.m.Y', strtotime('next Sunday', strtotime('07.05.2010')));
梦太阳 2024-09-07 18:44:45
/**
 *
 * @param \DateTime $date
 * @param $dayOfWeek - e.g Monday, Tuesday ...
 */
public function findNearestDayOfWeek(\DateTime $date, $dayOfWeek)
{
    $dayOfWeek = ucfirst($dayOfWeek);
    $daysOfWeek = array(
        'Monday',
        'Tuesday',
        'Wednesday',
        'Thursday',
        'Friday',
        'Saturday',
        'Sunday',
    );
    if(!in_array($dayOfWeek, $daysOfWeek)){

        throw new \InvalidArgumentException('Invalid day of week:'.$dayOfWeek);
    }
    if($date->format('l') == $dayOfWeek){

        return $date;
    }

    $previous = clone $date;
    $previous->modify('last '.$dayOfWeek);

    $next = clone $date;
    $next->modify('next '.$dayOfWeek);

    $previousDiff = $date->diff($previous);
    $nextDiff = $date->diff($next);

    $previousDiffDays = $previousDiff->format('%a');
    $nextDiffDays = $nextDiff->format('%a');

    if($previousDiffDays < $nextDiffDays){

        return $previous;
    }

    return $next;
}

或者,您可以创建一个地图,显示一周中哪几天更接近,例如,如果您在最接近的星期一到星期三之后,那么找到上一个星期一会更快,因为它比下星期一更接近。

/**
 *
 * @param \DateTime $date
 * @param $dayOfWeek - e.g Monday, Tuesday ...
 */
public function findNearestDayOfWeek(\DateTime $date, $dayOfWeek)
{
    $dayOfWeek = ucfirst($dayOfWeek);
    $daysOfWeek = array(
        'Monday',
        'Tuesday',
        'Wednesday',
        'Thursday',
        'Friday',
        'Saturday',
        'Sunday',
    );
    if(!in_array($dayOfWeek, $daysOfWeek)){

        throw new \InvalidArgumentException('Invalid day of week:'.$dayOfWeek);
    }
    if($date->format('l') == $dayOfWeek){

        return $date;
    }

    $previous = clone $date;
    $previous->modify('last '.$dayOfWeek);

    $next = clone $date;
    $next->modify('next '.$dayOfWeek);

    $previousDiff = $date->diff($previous);
    $nextDiff = $date->diff($next);

    $previousDiffDays = $previousDiff->format('%a');
    $nextDiffDays = $nextDiff->format('%a');

    if($previousDiffDays < $nextDiffDays){

        return $previous;
    }

    return $next;
}

Alternatively you could create a map of what days of weeks are closer, e.g if you're after closest Monday to Wednesday, it would be faster to just find the previous Monday given that it's closer than the next Monday.

べ繥欢鉨o。 2024-09-07 18:44:45

发布了几个答案,我不断看到解决方案可以为我提供一周中某一天的下一个实例或上一个实例但不是最接近的< /强>。为了解决这个问题,我想出了这个函数:

function closestDate($day){

    $day = ucfirst($day);
    if(date('l', time()) == $day)
        return date("Y-m-d", time());
    else if(abs(time()-strtotime('next '.$day)) < abs(time()-strtotime('last '.$day)))
        return date("Y-m-d", strtotime('next '.$day));
    else
        return date("Y-m-d", strtotime('last '.$day));

}

输入:一周中的一天(“星期日”、“星期一”等)

输出:如果我询问最近的“星期日”,而今天是:

  1. “星期日”: I将获得今天的日期
  2. “星期一”:我将获得昨天的日期
  3. “星期六:我将获得明天的日期

希望这有帮助:)

There are several answers posted and I keep seeing solutions that could give me either the next instance of a day of the week or the previous instance but not the closest. To address this, I came up with this function:

function closestDate($day){

    $day = ucfirst($day);
    if(date('l', time()) == $day)
        return date("Y-m-d", time());
    else if(abs(time()-strtotime('next '.$day)) < abs(time()-strtotime('last '.$day)))
        return date("Y-m-d", strtotime('next '.$day));
    else
        return date("Y-m-d", strtotime('last '.$day));

}

Input: a day of the week ("sunday", "Monday", etc.)

Output: If I asked for the nearest "sunday" and today is:

  1. "Sunday": I will get today's date
  2. "Monday": I will get yesterday's date
  3. "Saturday: I will get tomorrow's date

Hope this helps :)

怪我闹别瞎闹 2024-09-07 18:44:45

strtotime 很神奇

echo date("d/m/y", strtotime("next sunday", strtotime("07.05.2010")  ) );

strtotime is magical

echo date("d/m/y", strtotime("next sunday", strtotime("07.05.2010")  ) );
绝不放开 2024-09-07 18:44:45

只需使用 strtotime() 和一些小技巧即可完成此操作。

function findNearest($day, $date)
{
    return strtotime("next $day", strtotime("$date - 4 days"));
}

echo date('d.m.Y', findNearest("Sunday", "07.05.2010")); // 09.05.2010
echo               findNearest("Sunday", "07.05.2010");  // 1273377600
echo date('d.m.Y', findNearest("Sunday", "09.05.2010")); // 09.05.2010
echo               findNearest("Sunday", "09.05.2010");  // 1273377600
echo date('d.m.Y', findNearest("Sunday", "05.05.2010")); // 02.05.2010
echo               findNearest("Sunday", "05.05.2010");  // 1272772800

This can be done using only strtotime() and a little trickery.

function findNearest($day, $date)
{
    return strtotime("next $day", strtotime("$date - 4 days"));
}

echo date('d.m.Y', findNearest("Sunday", "07.05.2010")); // 09.05.2010
echo               findNearest("Sunday", "07.05.2010");  // 1273377600
echo date('d.m.Y', findNearest("Sunday", "09.05.2010")); // 09.05.2010
echo               findNearest("Sunday", "09.05.2010");  // 1273377600
echo date('d.m.Y', findNearest("Sunday", "05.05.2010")); // 02.05.2010
echo               findNearest("Sunday", "05.05.2010");  // 1272772800
孤独患者 2024-09-07 18:44:45

您也可以使用 Carbon 库

$date = Carbon::create(2015, 7, 2); // 2015-07-02

// To get the first day of the week 
$monday = $date->startOfWeek(); // 2015-06-29

$mondayTwoWeeksLater = $date->addWeek(2); // 2015-07-13

You can use Carbon library as well

$date = Carbon::create(2015, 7, 2); // 2015-07-02

// To get the first day of the week 
$monday = $date->startOfWeek(); // 2015-06-29

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