Calculating Facebook user’s total work experience
Well, solving it went faster than I expected. Here’s the code used to calculate a Facebook user’s total work experience. There’s no doubt an easier and prettier way to do this, but it works and that’s enough for a virgin coder like me. Note that the code below is just for the calculation, it does not contain the rest of the code dealing with authenticating with FB etc.
// this calculates the amount of total work experience automatically from FB
$totalWorkExperience = 0;
foreach($user_info['work'] as $employer)
{
$currentWorkStartDate = $employer['start_date'];
if ($employer['end_date'] == “0000-00″) {
$currentWorkEndDate = Date(‘Y-m’);
} else {
$currentWorkEndDate = $employer['end_date'];
}$dateStarted = new DateTime($currentWorkStartDate);
$dateEnded = new DateTime($currentWorkEndDate);
$interval = $dateStarted->diff($dateEnded);
$totalWorkExperience += $interval->days;}
// Figure out how many years the experience in days amounts to, rounding to the closed full year
$totalWorkExperienceInYears = round($totalWorkExperience/365);