#1
  1. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jun 2009
    Posts
    677
    Rep Power
    7

    Cleaner way to check dates?


    Well, just lookin to maybe clean up here. Simple goal is for a time-off request module to check if the date requested either falls on a weekend, or a holiday. I just got the holiday calculations done to build its list, and just wondering if there's a simple/quick way to impliment this with what I already have checking for weekends Any help is greatly appreciated. Thank you.

    PHP Code:
    function dateCount() {
      <?php
        $current 
    = new DateTime('', new DateTimeZone('America/Chicago'));
        
    $next = new DateTime('', new DateTimeZone('America/Chicago'));
        
    $next $next->add(new DateInterval('P1Y'));
        
    $eSet = new DateTime('@' easter_date($current->format('Y')));
        
    $cEaster $eSet->format('m/d/Y');
        
    $eSet = new DateTime('@' easter_date($next->format('Y')));
        
    $nEaster $eSet->format('m/d/Y');
        
    $mem = new DateTime('@' strtotime('last mon of may this year'));
        
    $cMem $mem->format('m/d/Y');
        
    $mem = new DateTime('@' strtotime('last mon of may next year'));
        
    $nMem $mem->format('m/d/Y');
        
    $lab = new DateTime('@' strtotime('first mon of sep this year'));
        
    $cLab $lab->format('m/d/Y');
        
    $lab = new DateTime('@' strtotime('first mon of sep next year'));
        
    $nLab $lab->format('m/d/Y');
        
    $thx = new DateTime('@' strtotime('fourth thu of nov this year'));
        
    $cThx $thx->format('m/d/Y');
        
    $thx = new DateTime('@' strtotime('fourth thu of nov next year'));
        
    $nThx $thx->format('m/d/Y');
        
    $afterThx = new DateTime('@' strtotime('fourth thu of nov this year'));
        
    $afterThx $afterThx->add(new DateInterval('P1D'));
        
    $cAfterThx $afterThx->format('m/d/Y');
        
    $afterThx = new DateTime('@' strtotime('fourth thu of nov next year'));
        
    $afterThx $afterThx->add(new DateInterval('P1D'));
        
    $nAfterThx $afterThx->format('m/d/Y');
      
    ?>
      var startDate = new Date(document.getElementById('start').value);
      var endDate = new Date(document.getElementById('end').value);
      var holiDates = [];
      holiDates[0] = new Date("01/01/<?php echo $current->format('Y'); ?>"); // New Year's Day
      holiDates[0] = new Date("01/01/<?php echo $next->format('Y'); ?>");
      holiDates[1] = new Date("<?php echo $cEaster?>"); // Easter
      holiDates[1] = new Date("<?php echo $nEaster?>");
      holiDates[2] = new Date("<?php echo $cMem?>"); // Memorial Day
      holiDates[2] = new Date("<?php echo $nMem?>");
      holiDates[2] = new Date("06/04/<?php echo $current->format('Y'); ?>"); // Independence Day
      holiDates[2] = new Date("06/04/<?php echo $next->format('Y'); ?>");
      holiDates[2] = new Date("<?php echo $cLab?>"); // Labor Day
      holiDates[2] = new Date("<?php echo $nLab?>");
      holiDates[2] = new Date("<?php echo $cThx?>"); // Thanksgiving
      holiDates[2] = new Date("<?php echo $nThx?>");
      holiDates[2] = new Date("<?php echo $cAfterThx?>"); // Day After Thanksgiving
      holiDates[2] = new Date("<?php echo $nAfterThx?>");
      holiDates[2] = new Date("12/24/<?php echo $current->format('Y'); ?>"); // Christmas Eve
      holiDates[2] = new Date("12/24/<?php echo $next->format('Y'); ?>");
      holiDates[2] = new Date("12/25/<?php echo $current->format('Y'); ?>"); // Christmas Day
      holiDates[2] = new Date("12/25/<?php echo $next->format('Y'); ?>");
      holiDates[2] = new Date("12/31/<?php echo $current->format('Y'); ?>"); // New Year's Eve
      holiDates[2] = new Date("12/31/<?php echo $next->format('Y'); ?>");
      var length = holiDates.length;
      var fullCount = 0;
      var paidCount = 0;
      var vacDays = 0;
      for (var i = 0; i < length; i++) {
        holiday = holiDates[i].getDay();
        if(holiDates[i] >= startDate && holiDates[i] <= endDate && holiday != 0 && holiday != 6) {
          paidCount++;
        }
      }
      while (startDate <= endDate) {
        var day = startDate.getDay();
        if(day != 0 && day != 6) {
          fullCount++; 
        }
        startDate.setDate(startDate.getDate() + 1); 
      }
      document.getElementById("holDays").innerHTML = paidCount || 0;
      vacDays = fullCount - paidCount;
      document.getElementById("vacDays").innerHTML = vacDays || 0;
    }
    EDIT: Fixed array numbers...
  2. #2
  3. Wiser? Not exactly.
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    May 2001
    Location
    Bonita Springs, FL
    Posts
    5,947
    Rep Power
    4033
    You could clean up your PHP that generates the dates by doing this:
    PHP Code:
    <?php 
        $chicago 
    = new DateTimeZone('America/Chicago');
        
    $thisYear date('Y');
        
    $nextYear $thisYear+1;
        
    $holidays = array(
            
    'jan 1st '.$thisYear
            
    'jan 1st '.$nextYear
            
    '@'.easter_date($thisYear)
            , 
    '@'.easter_date($nextYear)
            , 
    'last mon of may '.$thisYear
            
    'last mon of may '.$nextYear
            
    'jul 4th '.$thisYear
            
    'jul 4th '.$nextYear
            
    'first mon of sep '.$thisYear
            
    'first mon of sep '.$nextYear
            
    'fourth thu of nov '.$thisYear
            
    'fourth thu of nov '.$nextYear
            
    'fourth thu of nov '.$thisYear.' +1 day'
            
    'fourth thu of nov '.$nextYear.' +1 day'
            
    'dec 24th '.$thisYear
            
    'dec 24th '.$nextYear
            
    'dec 25th '.$thisYear
            
    'dec 25th '.$nextYear
            
    'dec 31st '.$thisYear
            
    'dec 31st '.$nextYear
        
    );
        echo 
    'var holiDates = [];'.PHP_EOL;
        foreach (
    $holidays as $desc){
            
    $dt = new DateTime($desc$chicago);
            echo 
    'holiDates.push(new Date('.$dt->getTimestamp().'*1000));'.PHP_EOL;
        }
    ?>
    Otherwise the JS code look ok to me. I had to change some of your date strings because for me, something like 'last mon of may next year' did not work as you were expecting, and returned the wrong date.
    Recycle your old CD's, don't just trash them



    If I helped you out, show some love with some reputation, or tip with Bitcoins to 1N645HfYf63UbcvxajLKiSKpYHAq2Zxud
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jun 2009
    Posts
    677
    Rep Power
    7
    Excellent and nice and clean. ^_^I know little in javascript, and can only edit n do lil creating. Thank you very much for the input, and one NICE layout.

IMN logo majestic logo threadwatch logo seochat tools logo