Jump to content

PHP date glitch


Go to solution Solved by Andy2024,

Recommended Posts

I have done a calendar at www.suescaravan.co.uk/availability the issue is its showing 2 x July's

 

this is the code

$month = '1';
        $year = date('Y');
          
        for($m=$month; $m<13; $m++) {
          
          $timestamp = mktime(0, 0, 0, $m, 1, $year);
          $daysInMonth = date("t", $timestamp);
          $firstDay = date("N", $timestamp);
        ?>
          <div class="col-xl-4 col-lg-6" data-aos="fade-up" data-aos-delay="100">
              <div class="rounded-3 bg-secondary-subtle">
                <h3 class="mb-0 p-2" align="center"><?php echo date('F', mktime(0,0,0,$m)); ?></h3>
                <table class="table table-bordered m-0" cellspacing="1">
                  <tr>
                    <th><center>Mon</center></th>
                    <th><center>Tue</center></th>
                    <th><center>Wed</center></th>
                    <th><center>Thu</center></th>
                    <th><center>Fri</center></th>
                    <th><center>Sat</center></th>
                    <th><center>Sun</center></th>
                  </tr>
                  
                  <?php
                  $dayCount = 1;
                  
                  echo "<tr>";
                  for ($i = 1; $i <= 7; $i++) {
                    
                    if ($i < $firstDay) {
                      echo "<td></td>";
                    }
                    else {
                        
                       if(strtotime("$year-$m-$dayCount") > strtotime(date('Y-m-d'))) {
                        $bgClass = 'class="table-success btn-date" style="cursor:pointer"';
                       } else {
                        $bgClass = 'class="table-success"';
                       }
                      $tdTitle = "Available";
                      
                      if(!empty($results)){
                      
                        if( in_array( date('Y-m-d', strtotime("$year-$m-$dayCount")), $booked) ) {
                          $bgClass = 'class="table-danger"';
                          $tdTitle = "Booked";
                        }
                        if(in_array( date('Y-m-d', strtotime("$year-$m-$dayCount")), $startDates) ) {
                          $bgClass = 'class="cal-checkin"';
                          $tdTitle = "Check In";
                        }
                        if(in_array( date('Y-m-d', strtotime("$year-$m-$dayCount")), $endDates) ) {
                          $bgClass = 'class="cal-checkout btn-date" style="cursor:pointer"';
                          $tdTitle = "Chesk Out";
                        }
                        if(in_array( date('Y-m-d', strtotime("$year-$m-$dayCount")), $changeover) ) {
                          $bgClass = 'class="table-danger nice-diagonal"';
                          $tdTitle = "Change Over";
                        }
                        
                      }
                      
                      echo "<td $bgClass title='$tdTitle' data-date='".date('d M Y', strtotime("$year-$m-$dayCount"))."' data-rawdate='".date('Y,m,d', strtotime("$year-$m-$dayCount"))."' data-bs-toggle='tooltip' data-bs-placement='top'><center>$dayCount</center></td>";
                      $dayCount++;
                    }
                  }
                  echo "</tr>";
          
                   while ($dayCount <= $daysInMonth) {
                     echo "<tr>";
                     
                     for ($i = 1; $i <= 7 && $dayCount <= $daysInMonth; $i++) {
                       if(strtotime("$year-$m-$dayCount") > strtotime(date('Y-m-d'))) {
                        $bgClass = 'class="table-success btn-date" style="cursor:pointer"';
                       } else {
                        $bgClass = 'class="table-success"';
                       }
                       $tdTitle = "Available";
                       
                       if(!empty($results)){
                       
                       
                         if( in_array( date('Y-m-d', strtotime("$year-$m-$dayCount")), $booked) ) {
                            $bgClass = 'class="table-danger"';
                            $tdTitle = "Booked";
                         }
                         if(in_array( date('Y-m-d', strtotime("$year-$m-$dayCount")), $startDates) ) {
                            $bgClass = 'class="cal-checkin"';
                            $tdTitle = "Check In";
                         }
                         if(in_array( date('Y-m-d', strtotime("$year-$m-$dayCount")), $endDates) ) {
                            $bgClass = 'class="cal-checkout btn-date" style="cursor:pointer"';
                            $tdTitle = "Chesk Out";
                         }
                         if(in_array( date('Y-m-d', strtotime("$year-$m-$dayCount")), $changeover) ) {
                            $bgClass = 'class="table-danger checkout"';
                            $tdTitle = "Change Over";
                         }
                       }
                       
                       
                       
                       echo "<td $bgClass title='$tdTitle' data-date='".date('d M Y', strtotime("$year-$m-$dayCount"))."' data-rawdate='".date('Y,m,d', strtotime("$year-$m-$dayCount"))."' data-bs-toggle='tooltip' data-bs-placement='top'><center>$dayCount</center></td>";
                       $dayCount++;
                     }
                     
                     echo "</tr>";
                   }
                  ?>
                </table>
            </div>
          </div>
        <?php
        }
        ?>

 

Ive tried changing 

<?php echo date('F', mktime(0,0,0,$m)); ?>

to

<?php echo date('F', mktime(0,0,0,$m,0,$year)); ?>

but its throwing everything out by a month.

 

Does anyone have any idea what is happening?

 

 

Link to comment
https://forums.phpfreaks.com/topic/328164-php-date-glitch/
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.