LinkedIn work history time The code I have works. I was just curious if there was an easier way to write the function.

Note: It is referenced inside of an object so you may need to change the $this variables.

public function workTime() {

    $startDate = new DateTime($this-&gt;start_date);
    $startPretty = date("F Y", strtotime($this-&gt;start_date));
    $endDate = new DateTime($this-&gt;end_date);
    $endPretty = date("F Y", strtotime($this-&gt;end_date));
    $current = $this-&gt;current; // boolean true/false        

    if($current) // currently works at this job
    {
        $endDate = new DateTime(); // current date/time
        $endPretty = 'Present';
    }

    $diff = $endDate-&gt;diff($startDate);

    if($diff-&gt;y = 0)
    {
        if($diff-&gt;m &gt; 1)
        {
            $string = $diff-&gt;m . ' months';
        }
        else
        {
            $string = '1 month';
        }
    }
    elseif ($diff-&gt;y = 1)
    {
        if($diff-&gt;m &gt; 1)
        {
            $string = '1 year ' . $diff-&gt;m . ' months';
        }
        else
        {
            $string = '1 year 1 month';
        }
    }
    else
    {
        if($diff-&gt;m &gt; 1)
        {
            $string = $diff-&gt;y . ' years ' . $diff-&gt;m . ' months';
        }
        else
        {
            $string = $diff-&gt;y . ' years 1 month';
        }
    }       
    $output = $startPretty . ' - ' . $endPretty . ' ( ' . $string . ' )';
    return $output;

}


Here is some sample data:


$this-&gt;start_date = '2013-05-01';
$this-&gt;end_date = '2014-12-01';



or you can make $this-&gt;end_date = null; and $current = true;

It currently outputs:


  May 2013 - Present ( 1 year 7 months )


which is what it should do. I'm just curious if this could be done cleaner/simpler.
If you already parse the date string using DateTime, then it's better to format using that resulting date object instead of parsing again with strtotime, for example:

$startDate = new DateTime($this-&gt;start_date);
$startPretty = $startDate-&gt;format('F Y');


If $current is true, then there's no need to parse $this-&gt;end_date.
You can move that to the else block instead:

if($current) // currently works at this job
{
    $endDate = new DateTime(); // current date/time
    $endPretty = 'Present';
}
else
{
    $endDate = new DateTime($this-&gt;end_date);
    $endPretty = $endDate-&gt;format('F Y');
}


These conditions don't do what you think they do:


if($diff-&gt;y = 0)
{
    // ...
}
elseif ($diff-&gt;y = 1)
{
    // ... 
}



The = operator assigns. This is not a check for equality.
The result of an assignment is the assigned value.
So the above statements are actually evaluated like this:


if(0)  // always false, $diff-&gt;y was set to 0, no matter what it was before
{
    // ...
}
elseif (1)  // always true, $diff-&gt;y was set to 1, no matter what it was before
{
    // ...
}



That is, first $diff-&gt;y is set to 0, the expression evaluates to false,
then in the elseif, $diff-&gt;y is set to 1, the expression evaluates to true.
To see the problem, set start_date to 2 or more years before end_date.
The result will always be 1 year difference instead of 2, 3 or more.

What you really wanted is the equals operator == like this:

if($diff-&gt;y == 0)
{
    // ...
}
elseif ($diff-&gt;y == 1)
{
    // ... 
}