检查日期是否是周末 PHP

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4802335/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 14:25:59  来源:igfitidea点击:

Checking if date is weekend PHP

phpdate

提问by JamieSellars

This function seems to only return false. Are any of you getting the same? I'm sure I'm overlooking something, however, fresh eyes and all that ...

这个函数似乎只返回false。你们中有人得到同样的吗?我敢肯定我忽略了一些东西,但是,新鲜的眼睛等等......

function isweekend($date){
    $date = strtotime($date);
    $date = date("l", $date);
    $date = strtolower($date);
    echo $date;
    if($date == "saturday" || $date == "sunday") {
        return "true";
    } else {
        return "false";
    }
}

I call the function using the following:

我使用以下方法调用该函数:

$isthisaweekend = isweekend('2011-01-01');

回答by ThiefMaster

If you have PHP >= 5.1:

如果您有 PHP >= 5.1:

function isWeekend($date) {
    return (date('N', strtotime($date)) >= 6);
}

otherwise:

除此以外:

function isWeekend($date) {
    $weekDay = date('w', strtotime($date));
    return ($weekDay == 0 || $weekDay == 6);
}

回答by Brian

Another way is to use the DateTimeclass, this way you can also specify the timezone. Note: PHP 5.3 or higher.

另一种方法是使用DateTime类,这样您也可以指定时区。注意:PHP 5.3 或更高版本。

// For the current date
function isTodayWeekend() {
    $currentDate = new DateTime("now", new DateTimeZone("Europe/Amsterdam"));
    return $currentDate->format('N') >= 6;
}

If you need to be able to check a certain date string, you can use DateTime::createFromFormat

如果您需要能够检查某个日期字符串,您可以使用DateTime::createFromFormat

function isWeekend($date) {
    $inputDate = DateTime::createFromFormat("d-m-Y", $date, new DateTimeZone("Europe/Amsterdam"));
    return $inputDate->format('N') >= 6;
}

The beauty of this way is that you can specify the timezone without changing the timezone globally in PHP, which might cause side-effects in other scripts (for ex. Wordpress).

这种方式的美妙之处在于您可以在不更改 PHP 中全局时区的情况下指定时区,这可能会导致其他脚本(例如 Wordpress)的副作用。

回答by Harvey

If you're using PHP 5.5 or PHP 7 above, you may want to use:

如果您使用 PHP 5.5 或 PHP 7 以上,您可能需要使用:

function isTodayWeekend() {
    return in_array(date("l"), ["Saturday", "Sunday"]);
}

and it will return "true" if today is weekend and "false" if not.

如果今天是周末,它将返回“true”,否则返回“false”。

回答by reko_t

Here:

这里:

function isweekend($year, $month, $day)
{
    $time = mktime(0, 0, 0, $month, $day, $year);
    $weekday = date('w', $time);
    return ($weekday == 0 || $weekday == 6);
}

回答by Hoàng Long

The working version of your code (from the errors pointed out by BoltClock):

代码的工作版本(来自 BoltClock 指出的错误):

<?php
$date = '2011-01-01';
$timestamp = strtotime($date);
$weekday= date("l", $timestamp );
$normalized_weekday = strtolower($weekday);
echo $normalized_weekday ;
if (($normalized_weekday == "saturday") || ($normalized_weekday == "sunday")) {
    echo "true";
} else {
    echo "false";
}

?>

The stray "{" is difficult to see, especially without a decent PHP editor (in my case). So I post the corrected version here.

杂散的“{”很难看到,尤其是没有像样的 PHP 编辑器(就我而言)。所以我在这里发布更正后的版本。

回答by boesing

For guys like me, who aren't minimalistic, there is a PECL extension called "intl". I use it for idn conversion since it works way better than the "idn" extension and some other n1 classes like "IntlDateFormatter".

对于像我这样不简约的人来说,有一个名为“ intl”的 PECL 扩展。我将它用于 idn 转换,因为它比“idn”扩展名和其他一些 n1 类(如“IntlDateFormatter”)工作得更好。

Well, what I want to say is, the "intl" extension has a class called "IntlCalendar" which can handle many international countries (e.g. in Saudi Arabia, sunday is not a weekend day). The IntlCalendar has a method IntlCalendar::isWeekendfor that. Maybe you guys give it a shot, I like that "it works for almost every country" fact on these intl-classes.

好吧,我想说的是,“intl”扩展有一个名为“IntlCalendar”的类,它可以处理许多国际国家(例如在沙特阿拉伯,星期日不是周末)。IntlCalendar 有一个方法IntlCalendar::isWeekend为此。也许你们试一试,我喜欢这些国际课程中“几乎适用于每个国家”的事实。

EDIT: Not quite sure but since PHP 5.5.0, the intl extension is bundled with PHP (--enable-intl).

编辑:不太确定,但自 PHP 5.5.0 起,intl 扩展与 PHP (--enable-intl) 捆绑在一起。

回答by Wynn

This works for me and is reusable.

这对我有用并且可以重复使用。

function isThisDayAWeekend($date) {

    $timestamp = strtotime($date);

    $weekday= date("l", $timestamp );

    if ($weekday =="Saturday" OR $weekday =="Sunday") { return true; } 
    else {return false; }

}