根据今天的日期的 PHP 日期范围

发布于 2024-09-12 03:59:00 字数 791 浏览 2 评论 0原文

任何人都可以想出一种更好的方法来将其写在循环中并获得相同的结果吗?

$today = date('l');

    if($today == 'Wednesday'){
        $min = date('l-m-d-y');
        $max = date('l-m-d-y', strtotime('+4 days'));
    }else if($today == 'Thursday'){
        $min = date('l-m-d-y', strtotime('-1 days'));
        $max = date('l-m-d-y', strtotime('+3 days'));
    }else if($today == 'Friday'){
        $min = date('l-m-d-y', strtotime('-2 days'));
        $max = date('l-m-d-y', strtotime('+2 days'));
    }else if($today == 'Saturday'){
        $min = date('l-m-d-y', strtotime('-2 days'));
        $max = date('l-m-d-y', strtotime('+1 days'));
    }else if($today == 'Sunday'){
        $min = date('l-m-d-y', strtotime('-3 days'));
        $max = date('l-m-d-y');
    }

    echo $min . ' - ' . $max;

Can anyone think of a better way of writing this out in a loop and getting the same result?

$today = date('l');

    if($today == 'Wednesday'){
        $min = date('l-m-d-y');
        $max = date('l-m-d-y', strtotime('+4 days'));
    }else if($today == 'Thursday'){
        $min = date('l-m-d-y', strtotime('-1 days'));
        $max = date('l-m-d-y', strtotime('+3 days'));
    }else if($today == 'Friday'){
        $min = date('l-m-d-y', strtotime('-2 days'));
        $max = date('l-m-d-y', strtotime('+2 days'));
    }else if($today == 'Saturday'){
        $min = date('l-m-d-y', strtotime('-2 days'));
        $max = date('l-m-d-y', strtotime('+1 days'));
    }else if($today == 'Sunday'){
        $min = date('l-m-d-y', strtotime('-3 days'));
        $max = date('l-m-d-y');
    }

    echo $min . ' - ' . $max;

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

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

发布评论

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

评论(2

梦里°也失望 2024-09-19 03:59:00

我假设您希望周六分钟内为 -3,周日分钟内为 -4。无论如何,这就是这个想法:

$weekday = date("w");
if ($weekday == 0)
    $weekday = 7;

if ($weekday >= 3) {
    $min = date('l-m-d-y',
        strtotime(($weekday==3?"+0":(3-$weekday))." days");
    $max = date('l-m-d-y',
        strtotime("+".(7-$weekday)." days");
}

I assumed you wanted -3 in the min on Saturday and -4 on Sunday. Anyway, this is the idea:

$weekday = date("w");
if ($weekday == 0)
    $weekday = 7;

if ($weekday >= 3) {
    $min = date('l-m-d-y',
        strtotime(($weekday==3?"+0":(3-$weekday))." days");
    $max = date('l-m-d-y',
        strtotime("+".(7-$weekday)." days");
}
放肆 2024-09-19 03:59:00

可以将其存储在一个数组中,以日期为键,+/-x 天作为值。

could store it in an array with the day as the key and the +/-x days as the values.

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