Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New to the CF scene
    Join Date
    Jun 2012
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Summing years between two dates

    Hey guys,

    I'm trying to build a search feature in my site that will allow an employer to search for a student based on the number of years of work experience they have. There are two categories of work experience: current and previous. So the code is supposed to get the total number of years of work experience for each student (current + previous), based on the work dates they have posted.

    So far, I have been able to obtain a piece of code that gets a rough estimate for the number of years between two dates. What I want is for the total number of years to be added and displayed in a table if both the current and previous work experience dates belong to the same student. This is my code so far...

    PHP Code:
    <?php 
                                  $workprevious 
    mysql_query("Select * from work_experience, students where students.stud_ID = work_experience.stud_ID AND work_type='previous'") or die (mysql_error());
                                  
    $row_workprevious mysql_fetch_assoc($workprevious);
                                  
    $date1 $row_workprevious['work_date_from'];
                                
    $date2 $row_workprevious['work_date_to'];
                                
    $diff abs(strtotime($date2) - strtotime($date1));
                                
    $years floor($diff / (365*60*60*24));
                                
                                  
                                  
    $workcurrent mysql_query("Select * from work_experience, students where students.stud_ID = work_experience.stud_ID AND work_type='current'") or die (mysql_error());
                                  
    $row_workcurrent mysql_fetch_assoc($workcurrent);
                                  
                                    
    $date3 $row_workcurrent['work_date_from'];
                                    
    $date4 $row_workcurrent['work_date_to'];
                                    
    $difference abs(strtotime($date3) - strtotime($date4));
                                    
    $year floor($difference / (365*60*60*24));
                                    echo 
    $years;
                                    
                                    if (
    $row_workprevious['stud_ID']==$row_workcurrent['stud_ID']) {
                                        
    $total $years $year;
                                        echo 
    $total;
                                    }
                                    elseif (
    $row_workprevious['stud_ID']==$row_workcurrent['stud_ID']) {
                                        echo 
    $year;
                                        echo 
    $years;
                                    }
                                  
    ?>

    Any assistance would be greatly appreciated as I'm still new in php

  • #2
    Senior Coder
    Join Date
    Jan 2011
    Location
    Missouri
    Posts
    4,441
    Thanks
    23
    Thanked 630 Times in 629 Posts
    Don't know how you have the dates formatted, but this code works to get the difference in time. It's set to show days, but can be changed:
    PHP Code:
    <?php
    $datetime1 
    date_create('2009-10-11');
    $datetime2 date_create('2009-10-13');
    $interval date_diff($datetime1$datetime2);
    echo 
    $interval->format('%a days');
    ?>


  •  

    Posting Permissions

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