How do I know if today's date is Saturday or Sunday (weekend) in PHP?

32

I want to know what is the simplest possible way to find out if today's date is Saturday or Sunday in PHP.

What are the possible ways to do this?

    
asked by Wallace Maxters 23.06.2016 в 19:55
source

7 answers

43

To find out if the current date is a weekend:

date( 'N' ) > 5       // hora local
gmdate( 'N' ) > 5     // GMT
  • The 'N' parameter returns the days of 1 to 7 , being 1 Monday, and 7 Sunday, and runs from PHP 5.1 onwards.

  • To do operations without taking into account the server's timezone , just use gmdate() instead of date() , in all examples.

Just curious, if you need the 'w' (tiny!) this is enough:

! ( date( 'w' ) % 6 )   

or even

( date( 'w' ) % 6 ) == 0
  • w returns the day of the week in the range of 0 to 6 , being 0 sunday and 6 sat.

  • >
  • The % is required because it is PHP, and the evaluation order is weird.

If you want to further simplify the 0 case, simply reverse the test:

echo date( 'w' ) % 6 ? 'Não é fim de semana' : 'é fim de semana';

See all the 6 parameters in the manual:

  

link

    
answered by 23.06.2016 / 19:59
source
11

If you have PHP > = 5.1:

function isWeekend($date) {
    return (date('N', strtotime($date)) >= 6);
}

else:

function isWeekend($date) {
    $weekDay = date('w', strtotime($date));
    return ($weekDay == 0 || $weekDay == 6);
}

Translated from link

    
answered by 23.06.2016 в 19:59
11
if (date('w') == 0 or date('w') == 6) {
    echo "É final de semana ";
}
    
answered by 23.06.2016 в 20:03
11

Another way of knowing this, is catching the day in full. As in English on Saturday ( saturday ) and sunday start with the letter S , just know that.

See:

if (substr(date('D'), 0, 1) === 'S') {

   echo 'final de semana';

}

If you are using PHP 5.4, you can delete substr() and replace with [0] , known as Function array dereferencing .

Example:

if (date('D')[0] === 'S') {

   echo 'final de semana';

}
    
answered by 23.06.2016 в 20:05
8

I will give my pitaco in my own answer:

in_array(date('w'), [0, 6])

In this case, w returns the day of the week in numeric format, where 0 is Sunday and 6 , Saturday.

in_array will do the job of checking if the value of the first parameter ( $needle , translating is "needle") is in the array that must be passed in the second parameter ( $haystack , which means " haystack ").

    
answered by 23.06.2016 в 20:08
5
<?php
$domingo = mktime(0, 0, 0, 6, 19, 2016); // 19/6/2016
$sabado = mktime(0, 0, 0, 7, 2, 2016); // 2/7/2016
$sexta = mktime(0, 0, 0, 6, 24, 2016); // 24/6/2016

echo "Data 01 : " . (fimDeSemana($domingo) ? "fim de semana" : "dia de semana");
echo "<br/>";
echo "Data 02 : " . (fimDeSemana($sabado) ? "fim de semana" : "dia de semana");
echo "<br/>";
echo "Data 03 : " . (fimDeSemana($sexta) ? "fim de semana" : "dia de semana");


function fimDeSemana($date)
{
    return date("D", $date) === "Sat" or date("D", $date) === "Sun";
}

Output:

Data 01 : fim de semana
Data 02 : fim de semana
Data 03 : dia de semana
    
answered by 23.06.2016 в 20:19
1

Using the class DateTime

$str = null;
//$str = '2016-09-24';
$dt = new DateTime($str);
$weekend = ($dt->format('w') % 6) == 0;
var_dump($weekend);
    
answered by 15.09.2016 в 12:30