SitePoint Sponsor

User Tag List

Results 1 to 10 of 10

Hybrid View

  1. #1
    SitePoint Guru
    Join Date
    Oct 2011
    Posts
    657
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Issue in count date from date range

    Hi...

    I encountered problem in my counting number of dates from date to date.

    for example :
    he file leave:

    Feb. 18, Saturday

    Feb. 20, Monday

    Feb. 21, Tuesday


    $DATE_LEAVE_FROM = 2012-02-18
    $DATE_LEAVE_TO = 2012-02-21

    Using this code:

    Code:
    <?php
      $DATE_LEAVE_FROM = $_GET['DATE_LEAVE_FROM'];
      $DATE_LEAVE_TO = $_GET['DATE_LEAVE_TO'];
      $DATE_FROM = strtotime($DATE_LEAVE_FROM, 0);
      $DATE_TO = strtotime($DATE_LEAVE_TO, 0);
                                                            
      $difference = ($DATE_TO - $DATE_FROM);
      $HOURS_LEAVE = floor($difference / 86400);
    ?>
    usiing this code the output is 4, because of the from date to date range which is correct.

    BUt I need to count only the date which is from Monday to Saturday or should I say don't count date which is Sunday..

    Is it possible?How?

    Thank you so much

  2. #2
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    It is possible but I do not have time to solve it for you.

    The thing to do is to loop through dates starting with DATE_LEAVE_FROM and keep adding one day until you find that you reach DATE_LEAVE_TO.

    In that loop you would do 2 other things:

    add one day to a counter
    if day of week is Sunday then skip that step

    You'd likely wrap that in a function you could use elsewhere.

    You could either use PHPs DateTime class (be very careful to check which functionality applies to your PHP version) or carry on using strtotime() with args like "+ 1 DAY" to keep moving the dates up by one.

  3. #3
    SitePoint Guru
    Join Date
    Oct 2011
    Posts
    657
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I tried this code:

    Code:
    <?php
    include 'config.php';
    session_start();
    $currentEmpID = $_SESSION['empID'];
    
    $sql = "SELECT em.EMP_NO, p.EMP_ID, CONCAT(LNAME, ', ',  FNAME, ' ', MI, '.') AS FULLNAME FROM PERSONAL p, EMPLOYMENT em WHERE p.EMP_ID='$currentEmpID' AND em.EMP_ID = '$currentEmpID' AND em.STATUS = 'Reg Operatives'";
    $recPersonal = $conn->Execute($sql);
    
    if (!$recPersonal) {
        print $conn->ErrorMsg();
    }
    
    if (!$recPersonal->BOF) {
        $recPersonal->MoveFirst();
    }
    
    
    $sql = "SELECT p.EMP_ID, CONCAT(LNAME, ', ' , FNAME, ' ', MI) AS FULLNAME FROM PERSONAL p, EMPLOYMENT em WHERE p.EMP_ID = em.EMP_ID AND em.STATUS = 'Reg Operatives' ORDER BY LNAME ASC";
    $recPersonalNav = $conn->GetAll($sql);
    $smarty->assign('personalAll', $recPersonalNav);
    // ========================================================================================================================
    
    //$sql = "SELECT em.EMP_NO, p.EMP_ID, CONCAT(LNAME, ', ',  FNAME, ' ', MI, '.') AS FULLNAME FROM PERSONAL p, EMPLOYMENT em  WHERE p.EMP_ID='$currentEmpID' AND em.EMP_ID = '$currentEmpID'";   
    $sql = "SELECT em.EMP_NO, p.EMP_ID, CONCAT(LNAME, ', ',  FNAME, ' ', MI, '.') AS FULLNAME FROM PERSONAL p, EMPLOYMENT em  WHERE p.EMP_ID='$currentEmpID' AND em.EMP_ID = '$currentEmpID' AND em.STATUS = 'Reg Operatives'";      
      
    $recPersonalHead = $conn->Execute($sql);
    $fullName = $recPersonalHead->fields["FULLNAME"];
    $empno = $recPersonalHead->fields["EMP_NO"];
    
    $smarty->assign('empid', $currentEmpID);
    $smarty->assign('fullname', $fullName);
    $smarty->assign('empno', $empno);
    
    //===================Select Leave Data===================
    
          $EMP_NO = $_GET['EMP_NO'];
          $NAME = $_GET['NAME'];
          $DATE_LEAVE_FROM = $_GET['DATE_LEAVE_FROM'];
          $DATE_LEAVE_TO = $_GET['DATE_LEAVE_TO'];
          $HOURS_LEAVE = $_GET['HOURS_LEAVE'];
          $Approve = $_GET['Approve'];
          $TYPE =$_GET['TYPE'];
          $dateprocess = $_GET['dateprocess'];
          $DATE_LEAVE = $_GET['DATE_LEAVE']; 
          $EMPNO = $_GET['EMPNO'];
          $DATE_FROM = strtotime($DATE_LEAVE_FROM, 0);
          $DATE_TO = strtotime($DATE_LEAVE_TO, 0);
                                                            
          $difference = ($DATE_TO - $DATE_FROM);
         // $HOURS_LEAVE = floor($difference / 86400);
    
    function GetDays($StartDate, $EndDate){   
    $StartDate = gmdate("Y-m-d", strtotime($StartDate));   
    $EndDate = gmdate("Y-m-d", strtotime($EndDate));   
    $Days[] = $StartDate;   
    $CurrentDate = $StartDate;   
      while($CurrentDate < $EndDate){   
        $CurrentDate = gmdate("Y-m-d", strtotime("+1 day", strtotime($CurrentDate)));   
            if(date("w",strtotime($CurrentDate))>0){ 
            $Days[] = $CurrentDate;   
            }   
      } 
    return count($Days);   
    }   
        
    $HOURS_LEAVE = GetDays($DATE_LEAVE_FROM, $DATE_LEAVE_TO); 
       
    $smarty->assign('LeaveStatus', array(SickLeave=>'Sick Leave',VacationLeave=>'Vacation Leave',BirthdayLeave=>'Birthday Leave',MaternityLeave=>'Maternity Leave', PaternityLeave=>'Paternity Leave', UnionLeave=>'Union Leave', Holiday=>'Holiday'));
    $smarty->assign('TYPE', $TYPE);
    
    $sql = "SELECT l.EMP_NO, l.DATE_LEAVE_FROM, 
    l.DATE_LEAVE_TO, l.HOURS_LEAVE, l.TYPE_LEAVE, l.STATUS_LEAVE 
    FROM $ADODB_DB.employment em, $PAYROLL.leave_data l 
    WHERE em.EMP_NO = l.EMP_NO AND em.EMP_ID = '$currentEmpID' AND l.DATE_LEAVE_FROM = '$DATE_LEAVE_FROM'";
    $result_edit = $conn2->Execute($sql);   
    
    $DATE_LEAVE_FROM = $result_edit->fields['DATE_LEAVE_FROM'];
    $DATE_LEAVE_TO = $result_edit->fields['DATE_LEAVE_TO'];
    
    $smarty->assign('DATE_LEAVE_FROM', $DATE_LEAVE_FROM);
    $smarty->assign('DATE_LEAVE_TO', $DATE_LEAVE_TO);
          
    $sql = "SELECT l.EMP_NO, p.EMP_ID, CONCAT(LNAME, ', ',  FNAME, ' ', MI, '.') AS FULLNAME, l.DATE_LEAVE_FROM, l.DATE_LEAVE_TO, l.HOURS_LEAVE, l.TYPE_LEAVE, l.STATUS_LEAVE FROM $ADODB_DB.PERSONAL p, $ADODB_DB.employment em, $PAYROLL.leave_data l WHERE em.EMP_NO = l.EMP_NO AND p.EMP_ID = em.EMP_ID ORDER BY FULLNAME";
    $rs = $conn2->GetAll($sql);
    
    $smarty->assign('getleave', $rs); 
    
    
    
    $smarty->display('header_att.tpl');
    $smarty->display('LeaveForm.tpl');
    $smarty->display('footer.tpl'); 
    //exit() ;
    ?>
    but still count 2012-02-18 to 2012-02-21 as 4 days.


    Thank you

  4. #4
    SitePoint Guru
    Join Date
    Oct 2011
    Posts
    657
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ok I while what is date('w') value..

    Thank you

  5. #5
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    PHP Code:
    function GetDays($StartDate$EndDate){   
    $StartDate gmdate("Y-m-d"strtotime($StartDate));   
    $EndDate gmdate("Y-m-d"strtotime($EndDate));   
    $Days[] = $StartDate;   
    $CurrentDate $StartDate;   
      while(
    $CurrentDate $EndDate){   
        
    $CurrentDate gmdate("Y-m-d"strtotime("+1 day"strtotime($CurrentDate)));   
            if(
    date("w",strtotime($CurrentDate))>0){ 
            
    $Days[] = $CurrentDate;   
            }   
      } 
    return 
    count($Days);   



    It might depend on when your system (or even your timezone?) starts the count for the weekday number.

    What day are you today, where you are and what does echo date("w"); give you? Is Monday 0 or is Sunday 0?

  6. #6
    SitePoint Guru
    Join Date
    Oct 2011
    Posts
    657
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Cups View Post
    PHP Code:
    function GetDays($StartDate$EndDate){   
    $StartDate gmdate("Y-m-d"strtotime($StartDate));   
    $EndDate gmdate("Y-m-d"strtotime($EndDate));   
    $Days[] = $StartDate;   
    $CurrentDate $StartDate;   
      while(
    $CurrentDate $EndDate){   
        
    $CurrentDate gmdate("Y-m-d"strtotime("+1 day"strtotime($CurrentDate)));   
            if(
    date("w",strtotime($CurrentDate))>0){ 
            
    $Days[] = $CurrentDate;   
            }   
      } 
    return 
    count($Days);   



    It might depend on when your system (or even your timezone?) starts the count for the weekday number.

    What day are you today, where you are and what does echo date("w"); give you? Is Monday 0 or is Sunday 0?

    when I echo date("w") it display 4,, is it means Thursday... because today is Thursday.

    Thank you

  7. #7
    SitePoint Guru
    Join Date
    Oct 2011
    Posts
    657
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    My timezone is (GMT + 8:00) Kuala Lumpur, Singapore..

    Thank you

  8. #8
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    Well the problem seems to be in that function somewhere:
    PHP Code:
            if(date("w",strtotime($CurrentDate))>0){  
            
    $Days[] = $CurrentDate;    
            } 
    change that part to this:

    PHP Code:
            if(date("w",strtotime($CurrentDate))>0){  
            
    $Days[] = $CurrentDate;    

             
    var_dump$CurrentDate );
             
    var_dumpdate("w",strtotime($CurrentDate)));

            } 
    And make sure your dates DO cover a Sunday, you should not see 0 as part of the output for day of week, it might throw up some other kind of anomoly

  9. #9
    SitePoint Guru
    Join Date
    Oct 2011
    Posts
    657
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I do what you suggest and here is my new code:
    Code:
    date_default_timezone_set('Asia/Singapore'); 
          $DATE_LEAVE_FROM = $_GET['DATE_LEAVE_FROM'];
          $DATE_LEAVE_TO = $_GET['DATE_LEAVE_TO'];
    function GetDays($StartDate, $EndDate){   
    $StartDate = date("Y-m-d", strtotime($StartDate));   
    $EndDate = date("Y-m-d", strtotime($EndDate));   
    $Days[] = $StartDate;   
    $CurrentDate = $StartDate;   
      while($CurrentDate < $EndDate){   
        $CurrentDate = date("Y-m-d", strtotime("+1 day", strtotime($CurrentDate)));   
            //if(date("w",strtotime($CurrentDate))>0){ 
           // $Days[] = $CurrentDate;   
            //}   
            
            if(date("w",strtotime($CurrentDate))>0){  
            $Days[] = $CurrentDate;    
    
             var_dump( $CurrentDate );
             var_dump( date("w",strtotime($CurrentDate)));
    
            }  
    
      } 
    return count($Days);    
    }   
        
    $HOURS_LEAVE = GetDays($DATE_LEAVE_FROM, $DATE_LEAVE_TO);
    the output is:

    string(10) "2012-02-20" string(1) "1" string(10) "2012-02-21" string(1) "2"

    Thank you so much

  10. #10
    SitePoint Guru
    Join Date
    Oct 2011
    Posts
    657
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thank you it works.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •