Filter days of week and just show working days in php

不问归期 提交于 2019-12-13 23:46:31

问题


I am trying to create a drop down with just the working days of the week on. Monday - Friday. Here's my code:

<?php if ($_SESSION['month'] == $current_month) { $current_day = date("j") + 1;} else {$current_day = 1;} ?>

<form action="" method="post">
  <select name="day" onchange="this.form.submit()">
    <option value="">-- Day --</option>
    <?php for ($i = $current_day; $i < 31; $i++) { ?>
    <option value="<?php echo $i; ?>" <?php echo $i == $_SESSION['day'] ? "selected='selected'":""; ?> >
    <?php $tmp_date = $_SESSION['year']."/".$_SESSION['month']."/".$i; 
          $weekday = date('D', strtotime($tmp_date)); 
          echo $weekday." "; ?>
    <?php echo $i; ?>
    </option>
    <?php } ?>
  </select>
</form>

This gives me the days of the week for the current month, but it shows all days. How can I only show Monday - Friday?


回答1:


It looks like $weekday is getting the names for you. Just do a nocase string compare:

      $weekday = date('D', strtotime($tmp_date)); 
      if (strcasecmp($weekday, 'Sun') != 0
          && strcasecmp($weekday, 'Sat') != 0){
          // Do something with valid days
      }



回答2:


<?php $tmp_date = $_SESSION['year']."/".$_SESSION['month']."/".$i; ?>
<?php if (!in_array(date('w', strtotime($tmp_date)), array(0, 6)) { ?>
<option value="<?php echo $i; ?>" <?php echo $i == $_SESSION['day'] ? "selected='selected'":""; ?> >

      $weekday = date('D', strtotime($tmp_date)); 
      echo $weekday." "; ?>
<?php echo $i; ?>
</option>
<?php } ?>



回答3:


This is a lot clearer then strtotime():

$start       = DateTime::createFromFormat('Y-n-j', $_SESSION['year'].'-'.$_SESSION['month'].'-01');
$daysInMonth = $start->format('t'); 
$end         = new DateTime("+{$daysInMonth} Days");
$interval    = new DateInterval('P1D');
$period      = new DatePeriod($start, $interval, $end);
foreach ($period as $day) {
    if (in_array($day->format('D'), array('Sat', 'Sun'))) continue;
    printf('<option value="%s"%s>%s %u</option>',
         $day->format('j'),
         ($_SESSION['day'] == $day->format('j')) ? ' selected' : '',
         $day->format('D'),
         $day->format('j')
    );
}

Demo




回答4:


This works for me:

<form action="" method="post">
  <select name="day" onchange="this.form.submit()">
    <option value="">-- Day --</option>
    <?php for ($i = $current_day; $i < 31; $i++) { 
    $tmp_date = $_SESSION['year']."/".$_SESSION['month']."/".$i; 
          $weekday = date('D', strtotime($tmp_date)); 
          if (strcasecmp($weekday, 'Sun') != 0
            && strcasecmp($weekday, 'Sat') != 0){ ?>
    <option value="<?php echo $i; ?>" <?php echo $i == $_SESSION['day'] ? "selected='selected'":""; ?> >
    <?php echo $weekday." ".$i; ?>
 <?php  } ?>


来源:https://stackoverflow.com/questions/23374040/filter-days-of-week-and-just-show-working-days-in-php

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!