33
I want to know 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?
33
I want to know 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?
46
To find out if the current date is weekend:
date( 'N' ) > 5 // hora local
gmdate( 'N' ) > 5 // GMT
Parameter 'N' returns the days of 1
to 7
, being 1 Monday, and 7 Sunday, and works from PHP 5.1 onwards.
Omitting the second parameter causes the date to return the current date/time, which is the default.
To make operations without taking into account the Timezone from the server, just use gmdate()
in place of date()
, in every instance.
Just out of curiosity, if you need the 'w' (minusculo!) that’s 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
Saturday.
The operator %
, of split rest, makes both the values 0
(Sunday) as 6
(Saturday) result in zero, which indicates weekend.
The ( )
are required as it is PHP, and the order of Evaluation is weird.
If you want to simplify the case of 'w'
, just reverse the test:
echo date( 'w' ) % 6 ? 'Não é fim de semana' : 'é fim de semana';
See in the manual all the parameters of date()
:
It’s the "simplest" way so far.
Congratulations on the explanation. I realize that this is missing in most questions. Giving the solution without teaching is not very good.
@Wallacemaxters later you get lazy reading :P
Beast this! I used a gambiarra at least ten times bigger :) Well explained
14
if (date('w') == 0 or date('w') == 6) {
echo "É final de semana ";
}
Now yes. ) +1
12
If you have PHP >= 5.1:
function isWeekend($date) {
return (date('N', strtotime($date)) >= 6);
}
but:
function isWeekend($date) {
$weekDay = date('w', strtotime($date));
return ($weekDay == 0 || $weekDay == 6);
}
Translated from https://stackoverflow.com/questions/4802335/checking-if-date-is-weekend-php
12
Another way to know this is by taking the day in full. As in English the Saturday (Saturday) and Sunday (Sunday) begin with the letter S
, just know this.
Behold:
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';
}
Nobody liked yours. It works, it will only force Microsoft to change the name of the next Windows because of you :P
@bigown wtf? hahah didn’t get the xD joke
I knew and have known :D http://chat.stackexchange.com/transcript/message/30568866#30568866 The joke is not with the answer, but with MS and the bad programmers: http://www.extremetech.com/computing/191279-why-is-it-called-windows-10-not-windows-9. And take a +1
Check it out, I was missing my +1. Was I prejudiced with the substr
? huehuehue
@Wallacemaxters! 11 for sure xD
Is there a problem or wrong information in the answer? can I correct!
9
I’ll give my pinch on my own answer:
in_array(date('w'), [0, 6])
In the case, w
returns the day of the week in numerical format, where 0
it is Sunday and 6
, saturday.
in_array
will do the job of checking if the value of the first parameter ($needle
, translating is "needle") lies in the array that must be passed in the second parameter ($haystack
, which means "haystack").
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";
}
Exit:
Data 01 : fim de semana
Data 02 : fim de semana
Data 03 : dia de semana
1
Using the class Datetime
$str = null;
//$str = '2016-09-24';
$dt = new DateTime($str);
$weekend = ($dt->format('w') % 6) == 0;
var_dump($weekend);
Browser other questions tagged php datetime date
You are not signed in. Login or sign up in order to post.
Define weekend :P Until you set, I say this because someone may consider otherwise.
– Maniero
If the guy doesn’t really know how to know if it’s the weekend, here at SOPT, he gets a lot of negatives :p
– Wallace Maxters
It is not so simple to set weekend, you gave the most accepted definition, there are others, and some may be more suitable in each situation..
– Maniero