# How do I know if a particular day is a weekend?

15

How do you know if an informed day, of the current month, fell on the weekend? For example:

``````echo isWeekend(24) // True
echo isWeekend(26) // False
``````

In the related topic below, I can know today using the `date` method:

``````date('w')
``````

But there is no function in `date` method for something like what I want.

I want to get a list with the total days of the month with `cal_days_in_month` and check day by day, if it is a weekend, you will receive differential treatment.

Related: #

asked by anonymous 26.07.2016 / 14:59

12

To create a date, you have the functions `mktime` and `gmmktime` :

``````mktime (
[ int \$hora [, int \$minuto [, int \$segundo[
, int \$mes [, int \$dia [, int \$ano [, int \$is_dst ]]]]]]]
)
``````

Knowing this, just use what's in the question:

``````\$dataescolhida = mktime ( 0, 0, 0, 5, 31, 2016 );

if( date( 'N', \$dataescolhida ) > 5 ) { ...
``````

Version with parameter `w` :

``````if( ( date( 'w', \$dataescolhida ) % 6 ) == 0 ) { ...
``````

The code above says if 05/31/2016 fell on a weekend.

Be careful with the order of the parameters, as PHP has no criteria, the month comes before the day.

More details at:

26.07.2016 / 15:09
6

`mktime` solves this problem, @Bacco has already said the essential, here is just one more example:

``````function finalDeSemana(\$aData) {
\$date = date('w', mktime(0, 0, 0, \$mes, \$dia, \$ano));

if (\$date == 6):
elseif (\$date == 0):
echo 'Domingo' . '<br>';
else:
echo 'Não é final de semana' . '<br>';
endif;
}

finalDeSemana('26/07/2016');
finalDeSemana('27/07/2016');
finalDeSemana('28/07/2016');
finalDeSemana('29/07/2016');
finalDeSemana('30/07/2016');
finalDeSemana('31/07/2016');
``````

Output:

``````Não é final de semana
Não é final de semana
Não é final de semana
Não é final de semana
Domingo
``````

26.07.2016 / 15:29
5

I do not know of any native method that returns if it's a weekend or not, but could be implemented through a simple function:

``````function isWeekend(\$dia) {
\$mes = date('n');
\$ano = date('Y');
\$dt = DateTime::createFromFormat('j/n/Y', "\$dia/\$mes/\$ano");
if ((date_format(\$dt, 'N') === '6') || (date_format(\$dt, 'N') === '7')) {
return true;
}else{
return false;
}
}
``````

Usage:

``````echo '<pre>';
var_dump(isWeekend(24));
echo '</pre>';

echo '<pre>';
var_dump(isWeekend(25));
echo '</pre>';

echo '<pre>';
var_dump(isWeekend(26));
echo '</pre>';
``````

Result for code rolled today (07/26/2016):

``````bool(true) // para dia 24/07/2016
bool(false) // para dia 25/07/2016
bool(false)// para dia 26/07/2016
``````

I hope I have helped!

26.07.2016 / 15:55
0

You can do that too.

``````<?php

function checkDayFDS(\$day){

\$nameDay = date("D", strtotime(\$day));
\$fds     = "Hoje não é final de semana<hr>";

if (\$nameDay == "Sat" || \$nameDay == "Sun"){
\$fds = "Oba, hoje é final de semana<hr>";
}

return \$fds;
}

echo checkDayFDS("2017-08-18");// Sexta