Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP if today is Saturday or Sunday

Tags:

php

I'm trying what should be a simple IF statement to show content if the day of the week is Saturday or Sunday. This statement always returns true.

<?php 
 if(date('D') == ('Sat' || 'Sun')) { 
      echo "Today is Saturday or Sunday.";
    } else {
      echo "Today is not Saturday or Sunday.";
    }
?>

I also tried: (Sat || Sun) and (Saturday || Sunday)

Any ideas here?

date('D') = Sun

like image 898
ahinkle Avatar asked Feb 13 '17 04:02

ahinkle


People also ask

How to check Today is Sunday or not in php?

php if(date('D') == 'Sat' || date('D') == 'Sun') { echo "Today is Saturday or Sunday."; } else { echo "Today is not Saturday or Sunday."; } ?> Output : Today is Saturday or Sunday.

How to check if a date is a Saturday in php?

PHP Code: <? php $dt='2011-01-04'; $dt1 = strtotime($dt); $dt2 = date("l", $dt1); $dt3 = strtolower($dt2); if(($dt3 == "saturday" )|| ($dt3 == "sunday")) { echo $dt3. ' is weekend'.

How to get Saturday and Sunday from date in php?

php $s = 'Monday, September 28, 2009'; $time = strtotime($s); $start = strtotime('last sunday, 12pm', $time); $end = strtotime('next saturday, 11:59am', $time); $format = 'l, F j, Y g:i A'; $start_day = date($format, $start); $end_day = date($format, $end); header('Content-Type: text/plain'); echo "Input: $s\nOutput: $ ...

How can I tell if php is today is Monday?

This should solve it: $day = date('D'); $date = date('d') if($day == Mon){ //Code for monday } if($date == 01){ //code for 1st fo the month } else{ //not the first, no money for you =/ }


2 Answers

The issue is, you have to check the condition individually.

Try this:

<?php
if(date('D') == 'Sat' || date('D') == 'Sun') { 
  echo "Today is Saturday or Sunday.";
} else {
  echo "Today is not Saturday or Sunday.";
}
?>

Explanation:

date() function with 'D' parameter will return the day like Sat, Sun etc

Output : Today is Saturday or Sunday.

Working code

date() parameters list

like image 68
Mayank Pandeyz Avatar answered Sep 20 '22 08:09

Mayank Pandeyz


You can also use date('N') where Sat = 6 and Sun = 7

<?php
if(date('N') > 5) { 
    echo "Today is Saturday or Sunday.";
} else {
    echo "Today is not Saturday or Sunday.";
}
?>
like image 39
RiggsFolly Avatar answered Sep 22 '22 08:09

RiggsFolly