Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Checking if date is weekend PHP

Tags:

date

php

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'); 
like image 808
JS1986 Avatar asked Jan 26 '11 07:01

JS1986


People also ask

How do you know if a date is Saturday or Sunday?

Use the getDay() method to check if a date is during the weekend, e.g. date. getDay() === 6 || date. getDay() === 0 . The method returns a number between 0 and 6 for the day of the week, where Sunday is 0 and Saturday is 6 .


1 Answers

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); } 
like image 50
ThiefMaster Avatar answered Oct 03 '22 06:10

ThiefMaster