1

I have a jquery ui datepicker with the weekends blocked off so people can't click them. I know you should never trust the users input and as the box can still be typed in they can still enter weekends. I tried this in php

if(date('w', strtotime($date)) == 6 || 'w', strtotime($date)) == 0)))) {
echo 'Event is on a weekday'; 
} else {
echo 'Event on a weekend';
}

Essentially, the or ||'s aren't actually being accessed for sundays (0) and it's only actually using saturday as the date.

can someone help?

thanks

user3013008
  • 21
  • 1
  • 6

3 Answers3

3

Try like

if(date('l', strtotime($date)) == 'Sunday' || date('l', strtotime($date)) == 'Saturday'))))  {
    echo 'Event on a weekend';
} else {
    echo 'Event is on a weekday'; 
}

Or even you can try like

if(date('w', strtotime($date)) == 6 || date('w', strtotime($date)) == 0) {
    echo 'Event on a weekend';
} else {
    echo 'Event is on a weekday'; 
}
Jens Timmerman
  • 9,316
  • 1
  • 42
  • 48
GautamD31
  • 28,552
  • 10
  • 64
  • 85
3

With DateTime::Format we can do as

$date = '2014-04-06' ; // Y-m-d 

$date = new DateTime($date);
$day =  $date->format("w");
if($day == 6 || $day == 0){
    echo 'Event on a weekend';
}else{
    echo 'Event on a weekday';
}
Abhik Chakraborty
  • 44,654
  • 6
  • 52
  • 63
1

You have a syntax error in your if statement in addition to getting the logic the wrong way around. It should be:

if(date('w', strtotime($date)) == 6 || date('w', strtotime($date)) == 0) {
    echo 'Event is on a weekend'; 
} else {
    echo 'Event on a weekday';
}
Pathik Vejani
  • 4,263
  • 8
  • 57
  • 98
Aleks G
  • 56,435
  • 29
  • 168
  • 265