Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 06-19-2012, 10:51 PM   PM User | #1
Legacial
New to the CF scene

 
Join Date: Jun 2012
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Legacial is an unknown quantity at this point
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
Legacial is offline   Reply With Quote
Old 06-20-2012, 03:21 PM   PM User | #2
sunfighter
Senior Coder

 
Join Date: Jan 2011
Location: Missouri
Posts: 2,383
Thanks: 18
Thanked 350 Times in 349 Posts
sunfighter is on a distinguished road
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');
?>
sunfighter is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 03:49 AM.


Advertisement
Log in to turn off these ads.