How do you know if a certain day is a weekend?

Asked

Viewed 1,229 times

16

How to 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 through the method date:

date('w')

But there is no function in the method date to something like what I want.

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

Related: How to know if today’s date is Saturday or Sunday (weekend) in PHP?

  • Knowing only the day can not tell if it fell on the weekend, there is something missing there.

  • I added, it could be the current month. @rray

  • @Bacco, I want to get a list of the total days of the month, cal_days_in_month and check day by day, if it is a weekend, receive differentiated treatment.

  • I replied giving a general idea, just apply the mktime in your loop. But when you do, put the whole problem to the question, because there may be better ways to do it. Always do your posts in the most complete way possible. This helps us help you. The answers are made on top of what is asked.

  • 2

    Relevant: http://answall.com/q/70473/132 - Take a look at my answer, as one of the things it addresses is the determination of the day of the week.

  • @Bacco was my distraction, I supplemented the question with the comment.

Show 1 more comment

4 answers

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 is in the question mentioned:

$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, because as PHP has no criterion, the month comes before the day.

More details on:

/a/136780/70

6

The mktime solves this problem, @Bacco already said the essential, I leave here just one more example:

function finalDeSemana($aData) {
   $dia = substr($aData, 0, 2);
   $mes = substr($aData, 3, 2);
   $ano = substr($aData, 6, 4);
   $date = date('w', mktime(0, 0, 0, $mes, $dia, $ano));

   if ($date == 6): 
      echo 'Sábado' . '<br>';
   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');

Exit:

Não é final de semana
Não é final de semana
Não é final de semana
Não é final de semana
Sábado
Domingo

5

I don’t know any native method that returns whether it is weekend or not, however, 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;
    }
}

Use:

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 run today (26/07/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’ve helped!

0

You can do so 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 
echo checkDayFDS("2017-08-19");// Sabado
echo checkDayFDS("2017-08-20");// Domingo
echo checkDayFDS("2017-08-21");// Segunda
?>
  • Take a look at this example: http://www.wsasystens.xyz/fds.php?dia=2017-08-19

Browser other questions tagged

You are not signed in. Login or sign up in order to post.