107

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 ...

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');
JS1986
  • 1,920
  • 4
  • 29
  • 50
  • 16
    Your `true` and `false` should not be quoted as strings. Also your code should give a parse error and not even run at all because of the stray `}` in your if. – BoltClock Jan 26 '11 at 07:54

8 Answers8

251

If you have 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);
}
ThiefMaster
  • 310,957
  • 84
  • 592
  • 636
  • 13
    If you use PHP < 5 then using `return date('w', strtotime($date)) % 6 == 0` is a little shorter. – Jim Jan 16 '12 at 14:25
  • 27
    While that does the same thing, I'd say the explicit check is more obvious for someone reading the code. – ThiefMaster Jan 16 '12 at 15:21
  • 2
    Then again that's what comments are for. – Stephen Sabatini Sep 12 '15 at 17:41
  • 16
    Well, there are people who say that code should be readable without a lot of comments. Good code is readable - and not necessarily short. #CleanCode – anweibel Jan 15 '16 at 08:54
  • The less code there is the less there is to test and maintain. But if its unreadable its harder to test and maintain. Does there have to be a hard and fast rule about readability, or is it down to experience? – jpkrc Jan 17 '18 at 19:06
  • 1
    Beware of variable type. date() returns string. Comparing it to number can create issues. Good practice could be to add a (int) and switch string to number – Albert S. Jul 10 '20 at 11:30
57

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

// 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

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).

Brian
  • 1,803
  • 1
  • 16
  • 22
  • 1
    Take your upvote, but the pedant in me is warning you when they decide to add an extra day of the week it might just be a weekday :) – Tricky Jun 27 '17 at 14:14
  • "this way you can also specify the timezone". According to [this SO post](https://stackoverflow.com/questions/470617/how-to-get-the-current-date-and-time-in-php/16814230), you can specify the timezone in alternate ways too. I've used your solution though – Wouter Vanherck Sep 24 '18 at 14:12
18

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

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

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

harveyhans
  • 459
  • 6
  • 12
11

Here:

function isweekend($year, $month, $day)
{
    $time = mktime(0, 0, 0, $month, $day, $year);
    $weekday = date('w', $time);
    return ($weekday == 0 || $weekday == 6);
}
reko_t
  • 55,302
  • 10
  • 87
  • 77
11

The working version of your code (from the errors pointed out by 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.

Hoàng Long
  • 10,746
  • 20
  • 75
  • 124
3

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".

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::isWeekend for that. Maybe you guys give it a shot, I like that "it works for almost every country" fact on these intl-classes.

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

boesing
  • 345
  • 3
  • 10
2

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; }

}
Nihal
  • 5,262
  • 7
  • 23
  • 41
Wynn
  • 183
  • 1
  • 2
1

As opposed to testing the explicit day of the week string or number, you can also test using the relative date this weekday of the supplied date.

A direct comparison between the values is not possible without a workaround, as the use of weekday resets the time of the supplied date to 00:00:00.0000.

DateTimeInterface objects

$date->setTime(0, 0, 0) != $date->modify('this weekday');

DateTimeInterface Method

A simple method to implement to ensure the supplied date object is not changed.

function isWeekend(DateTimeInterface $date): bool
{
    if ($date instanceof DateTime) {
        $date = DateTimeImmutable::createFromMutable($date);
    }

    return $date->setTime(0,0,0) != $date->modify('this weekday');
}

isWeekend(new DateTimeImmutable('Sunday')); //true

strtotime method

With strtotime you can compare with the date('Yz') format. If the Yz value changes between the supplied date and this weekday, the supplied date is not a weekday.

function isWeekend(string $date): bool
{
    return date('Yz', strtotime($dateValue)) != date('Yz', strtotime($dateValue . ' this weekday'));
}

isWeekend('Sunday'); //true

Example

https://3v4l.org/TSAVi

$sunday = new DateTimeImmutable('Sunday');
foreach (new DatePeriod($sunday, new DateInterval('P1D'), 6) as $date) {
    echo $date->format('D') . ' is' . (isWeekend($date) ? '' : ' not') . ' a weekend';
}

Result

Sun is a weekend
Mon is not a weekend
Tue is not a weekend
Wed is not a weekend
Thu is not a weekend
Fri is not a weekend
Sat is a weekend
Will B.
  • 17,883
  • 4
  • 67
  • 69