Thursday, January 22, 2015

PHP Tip - Check if a date is a weekend

Given a particular day, month, and year, you want to check if this date is a weekend. You can do so via the following helper function:

    function isWeekend($day, $month, $year)
    {
        //---set time zone---
        date_default_timezone_set('Asia/Singapore');
$time = mktime(0, 0, 0, $month, $day, $year);
$day = date('w', $time);
return ($day == 0 || $day == 6);
    }
    


This use this function, simply pass it the day, month, and year:
    
    if (isWeekend(23,1,2015)) {
        echo "Yeah, weekend!";
    } else {
        echo "Nah, weekday";
    }

No comments: