Viewed   117 times

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');

 Answers

5

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);
}
Tuesday, October 25, 2022
4

you need to escape the a and t as both have special meaning when used as formatting options in date()

echo date('M j at h:i a');

See it in action

Monday, October 17, 2022
 
sgohl
 
3

You don't need to change the php.ini file if you use date_default_timezone_set(). Just set it to the timezone you will be working in.

Something like this should go in a config file or on the page where you're working with dates (if it is only one page):

date_default_timezone_set('America/Los_Angeles');
Friday, December 23, 2022
 
5
var dayOfWeek = yourDateObject.getDay();
var isWeekend = (dayOfWeek === 6) || (dayOfWeek  === 0); // 6 = Saturday, 0 = Sunday
Saturday, November 5, 2022
4
$weekday = date("w", strtotime("2012-01-01"));

This will be 0 for Sunday through 6, Saturday.

http://www.php.net/manual/en/function.strtotime.php

http://php.net/manual/en/function.date.php

Sunday, October 16, 2022
 
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :