Formatting date difference in human-friendly terms

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;







up vote
1
down vote

favorite












I've written a function to take the difference between two dates and display it in a particular format. I've used a lot of if-else statements, which seems a little messy. Is there a way to shorten this up? Would using the ternary operator make sense?



$date1 = '2018-04-30 10:36:29';
$date2 = '2018-04-30 10:35:29';

echo dateDiff($date1, $date2);

function dateDiff($date1, $date2)

$date_1 = new DateTime($date1);
$date_2 = new DateTime($date2);

$diff = $date_1->diff($date_2);

if($diff->days > 365)
return $date_1->format('Y-m-d');


elseif($diff->days < 366 AND $diff->days > 7)
return $date_1->format('M d');


elseif($diff->days > 2 AND $diff->days < 8)
return $date_1->format('L - H:i');


elseif($diff->days == 2) return "Yesterday ".$date_1->format('H:i');

elseif($diff->days < 2 AND $diff->days > 0 OR $diff->days == 0 AND $diff->h > 1) return $date_1->format('H:i');

elseif($diff->days == 0 AND $diff->h < 1 AND $diff->i >= 1) return $diff->i." min ago";

elseif($diff->days == 0 AND $diff->h < 1 AND $diff->i < 1) return "just now";

else return $error = "Error!";








share|improve this question





















  • return $error = "Error!"; why?
    – hjpotter92
    Jan 12 at 17:40











  • Just wrote to test the code only. @hjpotter92
    – Otabek
    Jan 12 at 17:43










  • Many online sites (including this one) use the exact same feature you are trying to code. So it may prove useful to read their source code to see how they do it. This will certainly give you new ideas and you will be free to implement the one you like best.
    – Patrick Mevzek
    Jan 12 at 17:48
















up vote
1
down vote

favorite












I've written a function to take the difference between two dates and display it in a particular format. I've used a lot of if-else statements, which seems a little messy. Is there a way to shorten this up? Would using the ternary operator make sense?



$date1 = '2018-04-30 10:36:29';
$date2 = '2018-04-30 10:35:29';

echo dateDiff($date1, $date2);

function dateDiff($date1, $date2)

$date_1 = new DateTime($date1);
$date_2 = new DateTime($date2);

$diff = $date_1->diff($date_2);

if($diff->days > 365)
return $date_1->format('Y-m-d');


elseif($diff->days < 366 AND $diff->days > 7)
return $date_1->format('M d');


elseif($diff->days > 2 AND $diff->days < 8)
return $date_1->format('L - H:i');


elseif($diff->days == 2) return "Yesterday ".$date_1->format('H:i');

elseif($diff->days < 2 AND $diff->days > 0 OR $diff->days == 0 AND $diff->h > 1) return $date_1->format('H:i');

elseif($diff->days == 0 AND $diff->h < 1 AND $diff->i >= 1) return $diff->i." min ago";

elseif($diff->days == 0 AND $diff->h < 1 AND $diff->i < 1) return "just now";

else return $error = "Error!";








share|improve this question





















  • return $error = "Error!"; why?
    – hjpotter92
    Jan 12 at 17:40











  • Just wrote to test the code only. @hjpotter92
    – Otabek
    Jan 12 at 17:43










  • Many online sites (including this one) use the exact same feature you are trying to code. So it may prove useful to read their source code to see how they do it. This will certainly give you new ideas and you will be free to implement the one you like best.
    – Patrick Mevzek
    Jan 12 at 17:48












up vote
1
down vote

favorite









up vote
1
down vote

favorite











I've written a function to take the difference between two dates and display it in a particular format. I've used a lot of if-else statements, which seems a little messy. Is there a way to shorten this up? Would using the ternary operator make sense?



$date1 = '2018-04-30 10:36:29';
$date2 = '2018-04-30 10:35:29';

echo dateDiff($date1, $date2);

function dateDiff($date1, $date2)

$date_1 = new DateTime($date1);
$date_2 = new DateTime($date2);

$diff = $date_1->diff($date_2);

if($diff->days > 365)
return $date_1->format('Y-m-d');


elseif($diff->days < 366 AND $diff->days > 7)
return $date_1->format('M d');


elseif($diff->days > 2 AND $diff->days < 8)
return $date_1->format('L - H:i');


elseif($diff->days == 2) return "Yesterday ".$date_1->format('H:i');

elseif($diff->days < 2 AND $diff->days > 0 OR $diff->days == 0 AND $diff->h > 1) return $date_1->format('H:i');

elseif($diff->days == 0 AND $diff->h < 1 AND $diff->i >= 1) return $diff->i." min ago";

elseif($diff->days == 0 AND $diff->h < 1 AND $diff->i < 1) return "just now";

else return $error = "Error!";








share|improve this question













I've written a function to take the difference between two dates and display it in a particular format. I've used a lot of if-else statements, which seems a little messy. Is there a way to shorten this up? Would using the ternary operator make sense?



$date1 = '2018-04-30 10:36:29';
$date2 = '2018-04-30 10:35:29';

echo dateDiff($date1, $date2);

function dateDiff($date1, $date2)

$date_1 = new DateTime($date1);
$date_2 = new DateTime($date2);

$diff = $date_1->diff($date_2);

if($diff->days > 365)
return $date_1->format('Y-m-d');


elseif($diff->days < 366 AND $diff->days > 7)
return $date_1->format('M d');


elseif($diff->days > 2 AND $diff->days < 8)
return $date_1->format('L - H:i');


elseif($diff->days == 2) return "Yesterday ".$date_1->format('H:i');

elseif($diff->days < 2 AND $diff->days > 0 OR $diff->days == 0 AND $diff->h > 1) return $date_1->format('H:i');

elseif($diff->days == 0 AND $diff->h < 1 AND $diff->i >= 1) return $diff->i." min ago";

elseif($diff->days == 0 AND $diff->h < 1 AND $diff->i < 1) return "just now";

else return $error = "Error!";










share|improve this question












share|improve this question




share|improve this question








edited Jan 12 at 17:46









200_success

123k14143401




123k14143401









asked Jan 12 at 17:25









Otabek

169110




169110











  • return $error = "Error!"; why?
    – hjpotter92
    Jan 12 at 17:40











  • Just wrote to test the code only. @hjpotter92
    – Otabek
    Jan 12 at 17:43










  • Many online sites (including this one) use the exact same feature you are trying to code. So it may prove useful to read their source code to see how they do it. This will certainly give you new ideas and you will be free to implement the one you like best.
    – Patrick Mevzek
    Jan 12 at 17:48
















  • return $error = "Error!"; why?
    – hjpotter92
    Jan 12 at 17:40











  • Just wrote to test the code only. @hjpotter92
    – Otabek
    Jan 12 at 17:43










  • Many online sites (including this one) use the exact same feature you are trying to code. So it may prove useful to read their source code to see how they do it. This will certainly give you new ideas and you will be free to implement the one you like best.
    – Patrick Mevzek
    Jan 12 at 17:48















return $error = "Error!"; why?
– hjpotter92
Jan 12 at 17:40





return $error = "Error!"; why?
– hjpotter92
Jan 12 at 17:40













Just wrote to test the code only. @hjpotter92
– Otabek
Jan 12 at 17:43




Just wrote to test the code only. @hjpotter92
– Otabek
Jan 12 at 17:43












Many online sites (including this one) use the exact same feature you are trying to code. So it may prove useful to read their source code to see how they do it. This will certainly give you new ideas and you will be free to implement the one you like best.
– Patrick Mevzek
Jan 12 at 17:48




Many online sites (including this one) use the exact same feature you are trying to code. So it may prove useful to read their source code to see how they do it. This will certainly give you new ideas and you will be free to implement the one you like best.
– Patrick Mevzek
Jan 12 at 17:48










1 Answer
1






active

oldest

votes

















up vote
1
down vote



accepted










Chained ternary expressions in PHP are a major pain in the ass — don't use them!



You have a lot of tests that are redundant, since earlier tests will have already eliminated longer periods. The error case at the end also seems pointless.



Use consistent indentation and braces for readability and safety — please don't skimp.



function dateDiff($date1, $date2)

$date_1 = new DateTime($date1);
$date_2 = new DateTime($date2);
$diff = $date_1->diff($date_2);

if ($diff->days > 365)
return $date_1->format('Y-m-d');
elseif ($diff->days > 7)
return $date_1->format('M d');
elseif ($diff->days > 2)
return $date_1->format('L - H:i');
elseif ($diff->days == 2)
return "Yesterday ".$date_1->format('H:i');
elseif ($diff->days > 0 OR $diff->h > 1)
return $date_1->format('H:i');
elseif ($diff->i >= 1)
return $diff->i." min ago";
else
return "Just now";




Be sure to use consistent capitalization for "yesterday" and "just now".






share|improve this answer





















    Your Answer




    StackExchange.ifUsing("editor", function ()
    return StackExchange.using("mathjaxEditing", function ()
    StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix)
    StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
    );
    );
    , "mathjax-editing");

    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "196"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    convertImagesToLinks: false,
    noModals: false,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: null,
    bindNavPrevention: true,
    postfix: "",
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );








     

    draft saved


    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f184970%2fformatting-date-difference-in-human-friendly-terms%23new-answer', 'question_page');

    );

    Post as a guest






























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    1
    down vote



    accepted










    Chained ternary expressions in PHP are a major pain in the ass — don't use them!



    You have a lot of tests that are redundant, since earlier tests will have already eliminated longer periods. The error case at the end also seems pointless.



    Use consistent indentation and braces for readability and safety — please don't skimp.



    function dateDiff($date1, $date2)

    $date_1 = new DateTime($date1);
    $date_2 = new DateTime($date2);
    $diff = $date_1->diff($date_2);

    if ($diff->days > 365)
    return $date_1->format('Y-m-d');
    elseif ($diff->days > 7)
    return $date_1->format('M d');
    elseif ($diff->days > 2)
    return $date_1->format('L - H:i');
    elseif ($diff->days == 2)
    return "Yesterday ".$date_1->format('H:i');
    elseif ($diff->days > 0 OR $diff->h > 1)
    return $date_1->format('H:i');
    elseif ($diff->i >= 1)
    return $diff->i." min ago";
    else
    return "Just now";




    Be sure to use consistent capitalization for "yesterday" and "just now".






    share|improve this answer

























      up vote
      1
      down vote



      accepted










      Chained ternary expressions in PHP are a major pain in the ass — don't use them!



      You have a lot of tests that are redundant, since earlier tests will have already eliminated longer periods. The error case at the end also seems pointless.



      Use consistent indentation and braces for readability and safety — please don't skimp.



      function dateDiff($date1, $date2)

      $date_1 = new DateTime($date1);
      $date_2 = new DateTime($date2);
      $diff = $date_1->diff($date_2);

      if ($diff->days > 365)
      return $date_1->format('Y-m-d');
      elseif ($diff->days > 7)
      return $date_1->format('M d');
      elseif ($diff->days > 2)
      return $date_1->format('L - H:i');
      elseif ($diff->days == 2)
      return "Yesterday ".$date_1->format('H:i');
      elseif ($diff->days > 0 OR $diff->h > 1)
      return $date_1->format('H:i');
      elseif ($diff->i >= 1)
      return $diff->i." min ago";
      else
      return "Just now";




      Be sure to use consistent capitalization for "yesterday" and "just now".






      share|improve this answer























        up vote
        1
        down vote



        accepted







        up vote
        1
        down vote



        accepted






        Chained ternary expressions in PHP are a major pain in the ass — don't use them!



        You have a lot of tests that are redundant, since earlier tests will have already eliminated longer periods. The error case at the end also seems pointless.



        Use consistent indentation and braces for readability and safety — please don't skimp.



        function dateDiff($date1, $date2)

        $date_1 = new DateTime($date1);
        $date_2 = new DateTime($date2);
        $diff = $date_1->diff($date_2);

        if ($diff->days > 365)
        return $date_1->format('Y-m-d');
        elseif ($diff->days > 7)
        return $date_1->format('M d');
        elseif ($diff->days > 2)
        return $date_1->format('L - H:i');
        elseif ($diff->days == 2)
        return "Yesterday ".$date_1->format('H:i');
        elseif ($diff->days > 0 OR $diff->h > 1)
        return $date_1->format('H:i');
        elseif ($diff->i >= 1)
        return $diff->i." min ago";
        else
        return "Just now";




        Be sure to use consistent capitalization for "yesterday" and "just now".






        share|improve this answer













        Chained ternary expressions in PHP are a major pain in the ass — don't use them!



        You have a lot of tests that are redundant, since earlier tests will have already eliminated longer periods. The error case at the end also seems pointless.



        Use consistent indentation and braces for readability and safety — please don't skimp.



        function dateDiff($date1, $date2)

        $date_1 = new DateTime($date1);
        $date_2 = new DateTime($date2);
        $diff = $date_1->diff($date_2);

        if ($diff->days > 365)
        return $date_1->format('Y-m-d');
        elseif ($diff->days > 7)
        return $date_1->format('M d');
        elseif ($diff->days > 2)
        return $date_1->format('L - H:i');
        elseif ($diff->days == 2)
        return "Yesterday ".$date_1->format('H:i');
        elseif ($diff->days > 0 OR $diff->h > 1)
        return $date_1->format('H:i');
        elseif ($diff->i >= 1)
        return $diff->i." min ago";
        else
        return "Just now";




        Be sure to use consistent capitalization for "yesterday" and "just now".







        share|improve this answer













        share|improve this answer



        share|improve this answer











        answered Jan 12 at 17:57









        200_success

        123k14143401




        123k14143401






















             

            draft saved


            draft discarded


























             


            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f184970%2fformatting-date-difference-in-human-friendly-terms%23new-answer', 'question_page');

            );

            Post as a guest













































































            Popular posts from this blog

            Chat program with C++ and SFML

            Function to Return a JSON Like Objects Using VBA Collections and Arrays

            Will my employers contract hold up in court?