Subtracting two dates in php












21















I have got two dates in php



$date1 = 'May 3, 2012 10:38:22 GMT'

$date2 = '06 Apr 2012 07:22:21 GMT'


Then I subtract both of them



$date2 - $date1


, and get



Result:6


Why is the result 6 and not 27? ... ? How can I subtract the two dates, and make it return me a result based on month differences while subtracting the years & days & time ?










share|improve this question





























    21















    I have got two dates in php



    $date1 = 'May 3, 2012 10:38:22 GMT'

    $date2 = '06 Apr 2012 07:22:21 GMT'


    Then I subtract both of them



    $date2 - $date1


    , and get



    Result:6


    Why is the result 6 and not 27? ... ? How can I subtract the two dates, and make it return me a result based on month differences while subtracting the years & days & time ?










    share|improve this question



























      21












      21








      21


      12






      I have got two dates in php



      $date1 = 'May 3, 2012 10:38:22 GMT'

      $date2 = '06 Apr 2012 07:22:21 GMT'


      Then I subtract both of them



      $date2 - $date1


      , and get



      Result:6


      Why is the result 6 and not 27? ... ? How can I subtract the two dates, and make it return me a result based on month differences while subtracting the years & days & time ?










      share|improve this question
















      I have got two dates in php



      $date1 = 'May 3, 2012 10:38:22 GMT'

      $date2 = '06 Apr 2012 07:22:21 GMT'


      Then I subtract both of them



      $date2 - $date1


      , and get



      Result:6


      Why is the result 6 and not 27? ... ? How can I subtract the two dates, and make it return me a result based on month differences while subtracting the years & days & time ?







      php






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Sep 3 '13 at 20:31









      evan

      9,04352745




      9,04352745










      asked May 6 '12 at 7:55









      Dmitry MakovetskiydDmitry Makovetskiyd

      3,1312287148




      3,1312287148
























          7 Answers
          7






          active

          oldest

          votes


















          57














          Part 1: Why is the result 6?



          The dates are simply strings when you first subtract them. PHP attempts to convert them to integers. It does this by converting until the first non-number. So, date2 become 6 and date1 becomes 0.



          Part 2: How do you get it to work?



          $datetime1 = strtotime('May 3, 2012 10:38:22 GMT');
          $datetime2 = strtotime('06 Apr 2012 07:22:21 GMT');

          $secs = $datetime2 - $datetime1;// == <seconds between the two times>
          $days = $secs / 86400;


          Convert as appropriate.






          share|improve this answer


























          • Do I need to convert the seconds between two times..to days... isnt there a faster way

            – Dmitry Makovetskiyd
            May 6 '12 at 8:06











          • seconds in a day = 60*60*24 = 86,400 so just divide by that.

            – evan
            May 6 '12 at 8:06






          • 1





            Great solution @evan. Thumbs up (y)

            – NullPointer
            Dec 7 '13 at 6:42





















          12














          There is one way to use mktime n make the date in timestamp and then subtract and then use date function to show in the way u want....



          Other way is that format both of dates in the same format then subtract....



          Third way



          $date1=  new DateTime("May 3, 2012 10:38:22 GMT");
          $date2= new DateTime("06 Apr 2012 07:22:21 GMT");
          echo $date1->diff($date2)->("%d");


          forth way



          $datetime1 = strtotime('May 3, 2012 10:38:22 GMT');
          $datetime2 = strtotime('06 Apr 2012 07:22:21 GMT');
          $secs = $datetime2 - $datetime1;// == return sec in difference
          $days = $secs / 86400;





          share|improve this answer

































            11














            Using DateTime and DateInterval,



            $date1 = new DateTime("May 3, 2012 10:38:22 GMT");
            $date2 = new DateTime("06 Apr 2012 07:22:21 GMT");
            echo $date1->diff($date2)->format("%d");





            share|improve this answer


























            • This will return me the difference in days..cause thats what I want..to find the date difference

              – Dmitry Makovetskiyd
              May 6 '12 at 8:07











            • Isn't that what you want?

              – Shiplu Mokaddim
              May 6 '12 at 8:17











            • yeah, it is.....thanks

              – Dmitry Makovetskiyd
              May 6 '12 at 8:17











            • if the difference is more than a month, format("%d") would't account for that. So it's not perfect! Use echo $date1->diff($date2)->days; instead.

              – Imtiaz
              Jul 7 '18 at 20:57





















            5














            $todate= strtotime('May 3, 2012 10:38:22 GMT');
            $fromdate= strtotime('06 Apr 2012 07:22:21 GMT');
            $calculate_seconds = $todate- $fromdate; // Number of seconds between the two dates
            $days = floor($calculate_seconds / (24 * 60 * 60 )); // convert to days
            echo($days);


            This code will find the date difference between two dates..



            Here output is 27






            share|improve this answer

































              5














              Most of presented solutions seems to be working, but everyone forgets about one thing: time.



              Taking evan example:



              $datetime1 = strtotime('May 3, 2012 10:38:22 GMT');
              $datetime2 = strtotime('06 Apr 2012 07:22:21 GMT');

              $secs = $datetime2 - $datetime1;// == <seconds between the two times>
              $days = $secs / 86400;


              When you don't trim time part, what might lead to milscalculations. For example: Interval between 2014-05-01 14:00:00 (Y-m-d) and 2014-05-02 07:00:00 will be 0,xxx, not 1. You should trim time part of every date.



              So it should be:



              $datetime1 = strtotime(date('Y-m-d', strtotime('May 3, 2012 10:38:22 GMT')));
              $datetime2 = strtotime(date('Y-m-d', strtotime('06 Apr 2012 07:22:21 GMT')));

              $secs = $datetime2 - $datetime1;// == <seconds between the two times>
              $days = $secs / 86400;





              share|improve this answer































                0














                echo 'time'.$notification_time=  "2008-12-13 10:42:00";
                date_default_timezone_set('Asia/Kolkata');
                echo 'cureen'.$currenttime=date('Y-m-d H:i:s');
                $now = new DateTime("$notification_time");
                $ref = new DateTime("$currenttime");
                $diff = $now->diff($ref);
                printf('%d days, %d hours, %d minutes', $diff->d, $diff->h, $diff->i);





                share|improve this answer































                  0














                  If you want to use diff(it returns a Dateinterval object) method, the correct way is to format with %a. I mean:



                  If you check http://php.net/manual/en/dateinterval.format.php



                  The correct way is:



                   echo $date1->diff($date2)->format("%a");


                  For getting all days






                  share|improve this answer


























                  • a method name was omitted, correct is: echo $date1->diff($date2)->format("%a");

                    – Petr Sobotka
                    Jul 14 '17 at 20:54













                  • you are true, I will edit my response. Thanks

                    – Daniel Nieto
                    Jul 19 '17 at 10:20











                  Your Answer






                  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: "1"
                  };
                  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',
                  autoActivateHeartbeat: false,
                  convertImagesToLinks: true,
                  noModals: true,
                  showLowRepImageUploadWarning: true,
                  reputationToPostImages: 10,
                  bindNavPrevention: true,
                  postfix: "",
                  imageUploader: {
                  brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
                  contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
                  allowUrls: true
                  },
                  onDemand: true,
                  discardSelector: ".discard-answer"
                  ,immediatelyShowMarkdownHelp:true
                  });


                  }
                  });














                  draft saved

                  draft discarded


















                  StackExchange.ready(
                  function () {
                  StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f10469037%2fsubtracting-two-dates-in-php%23new-answer', 'question_page');
                  }
                  );

                  Post as a guest















                  Required, but never shown

























                  7 Answers
                  7






                  active

                  oldest

                  votes








                  7 Answers
                  7






                  active

                  oldest

                  votes









                  active

                  oldest

                  votes






                  active

                  oldest

                  votes









                  57














                  Part 1: Why is the result 6?



                  The dates are simply strings when you first subtract them. PHP attempts to convert them to integers. It does this by converting until the first non-number. So, date2 become 6 and date1 becomes 0.



                  Part 2: How do you get it to work?



                  $datetime1 = strtotime('May 3, 2012 10:38:22 GMT');
                  $datetime2 = strtotime('06 Apr 2012 07:22:21 GMT');

                  $secs = $datetime2 - $datetime1;// == <seconds between the two times>
                  $days = $secs / 86400;


                  Convert as appropriate.






                  share|improve this answer


























                  • Do I need to convert the seconds between two times..to days... isnt there a faster way

                    – Dmitry Makovetskiyd
                    May 6 '12 at 8:06











                  • seconds in a day = 60*60*24 = 86,400 so just divide by that.

                    – evan
                    May 6 '12 at 8:06






                  • 1





                    Great solution @evan. Thumbs up (y)

                    – NullPointer
                    Dec 7 '13 at 6:42


















                  57














                  Part 1: Why is the result 6?



                  The dates are simply strings when you first subtract them. PHP attempts to convert them to integers. It does this by converting until the first non-number. So, date2 become 6 and date1 becomes 0.



                  Part 2: How do you get it to work?



                  $datetime1 = strtotime('May 3, 2012 10:38:22 GMT');
                  $datetime2 = strtotime('06 Apr 2012 07:22:21 GMT');

                  $secs = $datetime2 - $datetime1;// == <seconds between the two times>
                  $days = $secs / 86400;


                  Convert as appropriate.






                  share|improve this answer


























                  • Do I need to convert the seconds between two times..to days... isnt there a faster way

                    – Dmitry Makovetskiyd
                    May 6 '12 at 8:06











                  • seconds in a day = 60*60*24 = 86,400 so just divide by that.

                    – evan
                    May 6 '12 at 8:06






                  • 1





                    Great solution @evan. Thumbs up (y)

                    – NullPointer
                    Dec 7 '13 at 6:42
















                  57












                  57








                  57







                  Part 1: Why is the result 6?



                  The dates are simply strings when you first subtract them. PHP attempts to convert them to integers. It does this by converting until the first non-number. So, date2 become 6 and date1 becomes 0.



                  Part 2: How do you get it to work?



                  $datetime1 = strtotime('May 3, 2012 10:38:22 GMT');
                  $datetime2 = strtotime('06 Apr 2012 07:22:21 GMT');

                  $secs = $datetime2 - $datetime1;// == <seconds between the two times>
                  $days = $secs / 86400;


                  Convert as appropriate.






                  share|improve this answer















                  Part 1: Why is the result 6?



                  The dates are simply strings when you first subtract them. PHP attempts to convert them to integers. It does this by converting until the first non-number. So, date2 become 6 and date1 becomes 0.



                  Part 2: How do you get it to work?



                  $datetime1 = strtotime('May 3, 2012 10:38:22 GMT');
                  $datetime2 = strtotime('06 Apr 2012 07:22:21 GMT');

                  $secs = $datetime2 - $datetime1;// == <seconds between the two times>
                  $days = $secs / 86400;


                  Convert as appropriate.







                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited May 6 '12 at 8:09

























                  answered May 6 '12 at 8:03









                  evanevan

                  9,04352745




                  9,04352745













                  • Do I need to convert the seconds between two times..to days... isnt there a faster way

                    – Dmitry Makovetskiyd
                    May 6 '12 at 8:06











                  • seconds in a day = 60*60*24 = 86,400 so just divide by that.

                    – evan
                    May 6 '12 at 8:06






                  • 1





                    Great solution @evan. Thumbs up (y)

                    – NullPointer
                    Dec 7 '13 at 6:42





















                  • Do I need to convert the seconds between two times..to days... isnt there a faster way

                    – Dmitry Makovetskiyd
                    May 6 '12 at 8:06











                  • seconds in a day = 60*60*24 = 86,400 so just divide by that.

                    – evan
                    May 6 '12 at 8:06






                  • 1





                    Great solution @evan. Thumbs up (y)

                    – NullPointer
                    Dec 7 '13 at 6:42



















                  Do I need to convert the seconds between two times..to days... isnt there a faster way

                  – Dmitry Makovetskiyd
                  May 6 '12 at 8:06





                  Do I need to convert the seconds between two times..to days... isnt there a faster way

                  – Dmitry Makovetskiyd
                  May 6 '12 at 8:06













                  seconds in a day = 60*60*24 = 86,400 so just divide by that.

                  – evan
                  May 6 '12 at 8:06





                  seconds in a day = 60*60*24 = 86,400 so just divide by that.

                  – evan
                  May 6 '12 at 8:06




                  1




                  1





                  Great solution @evan. Thumbs up (y)

                  – NullPointer
                  Dec 7 '13 at 6:42







                  Great solution @evan. Thumbs up (y)

                  – NullPointer
                  Dec 7 '13 at 6:42















                  12














                  There is one way to use mktime n make the date in timestamp and then subtract and then use date function to show in the way u want....



                  Other way is that format both of dates in the same format then subtract....



                  Third way



                  $date1=  new DateTime("May 3, 2012 10:38:22 GMT");
                  $date2= new DateTime("06 Apr 2012 07:22:21 GMT");
                  echo $date1->diff($date2)->("%d");


                  forth way



                  $datetime1 = strtotime('May 3, 2012 10:38:22 GMT');
                  $datetime2 = strtotime('06 Apr 2012 07:22:21 GMT');
                  $secs = $datetime2 - $datetime1;// == return sec in difference
                  $days = $secs / 86400;





                  share|improve this answer






























                    12














                    There is one way to use mktime n make the date in timestamp and then subtract and then use date function to show in the way u want....



                    Other way is that format both of dates in the same format then subtract....



                    Third way



                    $date1=  new DateTime("May 3, 2012 10:38:22 GMT");
                    $date2= new DateTime("06 Apr 2012 07:22:21 GMT");
                    echo $date1->diff($date2)->("%d");


                    forth way



                    $datetime1 = strtotime('May 3, 2012 10:38:22 GMT');
                    $datetime2 = strtotime('06 Apr 2012 07:22:21 GMT');
                    $secs = $datetime2 - $datetime1;// == return sec in difference
                    $days = $secs / 86400;





                    share|improve this answer




























                      12












                      12








                      12







                      There is one way to use mktime n make the date in timestamp and then subtract and then use date function to show in the way u want....



                      Other way is that format both of dates in the same format then subtract....



                      Third way



                      $date1=  new DateTime("May 3, 2012 10:38:22 GMT");
                      $date2= new DateTime("06 Apr 2012 07:22:21 GMT");
                      echo $date1->diff($date2)->("%d");


                      forth way



                      $datetime1 = strtotime('May 3, 2012 10:38:22 GMT');
                      $datetime2 = strtotime('06 Apr 2012 07:22:21 GMT');
                      $secs = $datetime2 - $datetime1;// == return sec in difference
                      $days = $secs / 86400;





                      share|improve this answer















                      There is one way to use mktime n make the date in timestamp and then subtract and then use date function to show in the way u want....



                      Other way is that format both of dates in the same format then subtract....



                      Third way



                      $date1=  new DateTime("May 3, 2012 10:38:22 GMT");
                      $date2= new DateTime("06 Apr 2012 07:22:21 GMT");
                      echo $date1->diff($date2)->("%d");


                      forth way



                      $datetime1 = strtotime('May 3, 2012 10:38:22 GMT');
                      $datetime2 = strtotime('06 Apr 2012 07:22:21 GMT');
                      $secs = $datetime2 - $datetime1;// == return sec in difference
                      $days = $secs / 86400;






                      share|improve this answer














                      share|improve this answer



                      share|improve this answer








                      edited May 6 '12 at 8:15

























                      answered May 6 '12 at 8:05









                      ajmal iqbalajmal iqbal

                      1294




                      1294























                          11














                          Using DateTime and DateInterval,



                          $date1 = new DateTime("May 3, 2012 10:38:22 GMT");
                          $date2 = new DateTime("06 Apr 2012 07:22:21 GMT");
                          echo $date1->diff($date2)->format("%d");





                          share|improve this answer


























                          • This will return me the difference in days..cause thats what I want..to find the date difference

                            – Dmitry Makovetskiyd
                            May 6 '12 at 8:07











                          • Isn't that what you want?

                            – Shiplu Mokaddim
                            May 6 '12 at 8:17











                          • yeah, it is.....thanks

                            – Dmitry Makovetskiyd
                            May 6 '12 at 8:17











                          • if the difference is more than a month, format("%d") would't account for that. So it's not perfect! Use echo $date1->diff($date2)->days; instead.

                            – Imtiaz
                            Jul 7 '18 at 20:57


















                          11














                          Using DateTime and DateInterval,



                          $date1 = new DateTime("May 3, 2012 10:38:22 GMT");
                          $date2 = new DateTime("06 Apr 2012 07:22:21 GMT");
                          echo $date1->diff($date2)->format("%d");





                          share|improve this answer


























                          • This will return me the difference in days..cause thats what I want..to find the date difference

                            – Dmitry Makovetskiyd
                            May 6 '12 at 8:07











                          • Isn't that what you want?

                            – Shiplu Mokaddim
                            May 6 '12 at 8:17











                          • yeah, it is.....thanks

                            – Dmitry Makovetskiyd
                            May 6 '12 at 8:17











                          • if the difference is more than a month, format("%d") would't account for that. So it's not perfect! Use echo $date1->diff($date2)->days; instead.

                            – Imtiaz
                            Jul 7 '18 at 20:57
















                          11












                          11








                          11







                          Using DateTime and DateInterval,



                          $date1 = new DateTime("May 3, 2012 10:38:22 GMT");
                          $date2 = new DateTime("06 Apr 2012 07:22:21 GMT");
                          echo $date1->diff($date2)->format("%d");





                          share|improve this answer















                          Using DateTime and DateInterval,



                          $date1 = new DateTime("May 3, 2012 10:38:22 GMT");
                          $date2 = new DateTime("06 Apr 2012 07:22:21 GMT");
                          echo $date1->diff($date2)->format("%d");






                          share|improve this answer














                          share|improve this answer



                          share|improve this answer








                          edited Mar 10 '18 at 19:04









                          nietonfir

                          4,02552539




                          4,02552539










                          answered May 6 '12 at 8:03









                          Shiplu MokaddimShiplu Mokaddim

                          44.2k10102166




                          44.2k10102166













                          • This will return me the difference in days..cause thats what I want..to find the date difference

                            – Dmitry Makovetskiyd
                            May 6 '12 at 8:07











                          • Isn't that what you want?

                            – Shiplu Mokaddim
                            May 6 '12 at 8:17











                          • yeah, it is.....thanks

                            – Dmitry Makovetskiyd
                            May 6 '12 at 8:17











                          • if the difference is more than a month, format("%d") would't account for that. So it's not perfect! Use echo $date1->diff($date2)->days; instead.

                            – Imtiaz
                            Jul 7 '18 at 20:57





















                          • This will return me the difference in days..cause thats what I want..to find the date difference

                            – Dmitry Makovetskiyd
                            May 6 '12 at 8:07











                          • Isn't that what you want?

                            – Shiplu Mokaddim
                            May 6 '12 at 8:17











                          • yeah, it is.....thanks

                            – Dmitry Makovetskiyd
                            May 6 '12 at 8:17











                          • if the difference is more than a month, format("%d") would't account for that. So it's not perfect! Use echo $date1->diff($date2)->days; instead.

                            – Imtiaz
                            Jul 7 '18 at 20:57



















                          This will return me the difference in days..cause thats what I want..to find the date difference

                          – Dmitry Makovetskiyd
                          May 6 '12 at 8:07





                          This will return me the difference in days..cause thats what I want..to find the date difference

                          – Dmitry Makovetskiyd
                          May 6 '12 at 8:07













                          Isn't that what you want?

                          – Shiplu Mokaddim
                          May 6 '12 at 8:17





                          Isn't that what you want?

                          – Shiplu Mokaddim
                          May 6 '12 at 8:17













                          yeah, it is.....thanks

                          – Dmitry Makovetskiyd
                          May 6 '12 at 8:17





                          yeah, it is.....thanks

                          – Dmitry Makovetskiyd
                          May 6 '12 at 8:17













                          if the difference is more than a month, format("%d") would't account for that. So it's not perfect! Use echo $date1->diff($date2)->days; instead.

                          – Imtiaz
                          Jul 7 '18 at 20:57







                          if the difference is more than a month, format("%d") would't account for that. So it's not perfect! Use echo $date1->diff($date2)->days; instead.

                          – Imtiaz
                          Jul 7 '18 at 20:57













                          5














                          $todate= strtotime('May 3, 2012 10:38:22 GMT');
                          $fromdate= strtotime('06 Apr 2012 07:22:21 GMT');
                          $calculate_seconds = $todate- $fromdate; // Number of seconds between the two dates
                          $days = floor($calculate_seconds / (24 * 60 * 60 )); // convert to days
                          echo($days);


                          This code will find the date difference between two dates..



                          Here output is 27






                          share|improve this answer






























                            5














                            $todate= strtotime('May 3, 2012 10:38:22 GMT');
                            $fromdate= strtotime('06 Apr 2012 07:22:21 GMT');
                            $calculate_seconds = $todate- $fromdate; // Number of seconds between the two dates
                            $days = floor($calculate_seconds / (24 * 60 * 60 )); // convert to days
                            echo($days);


                            This code will find the date difference between two dates..



                            Here output is 27






                            share|improve this answer




























                              5












                              5








                              5







                              $todate= strtotime('May 3, 2012 10:38:22 GMT');
                              $fromdate= strtotime('06 Apr 2012 07:22:21 GMT');
                              $calculate_seconds = $todate- $fromdate; // Number of seconds between the two dates
                              $days = floor($calculate_seconds / (24 * 60 * 60 )); // convert to days
                              echo($days);


                              This code will find the date difference between two dates..



                              Here output is 27






                              share|improve this answer















                              $todate= strtotime('May 3, 2012 10:38:22 GMT');
                              $fromdate= strtotime('06 Apr 2012 07:22:21 GMT');
                              $calculate_seconds = $todate- $fromdate; // Number of seconds between the two dates
                              $days = floor($calculate_seconds / (24 * 60 * 60 )); // convert to days
                              echo($days);


                              This code will find the date difference between two dates..



                              Here output is 27







                              share|improve this answer














                              share|improve this answer



                              share|improve this answer








                              edited May 27 '13 at 10:48

























                              answered May 27 '13 at 10:42









                              Romancha KCRomancha KC

                              1,147119




                              1,147119























                                  5














                                  Most of presented solutions seems to be working, but everyone forgets about one thing: time.



                                  Taking evan example:



                                  $datetime1 = strtotime('May 3, 2012 10:38:22 GMT');
                                  $datetime2 = strtotime('06 Apr 2012 07:22:21 GMT');

                                  $secs = $datetime2 - $datetime1;// == <seconds between the two times>
                                  $days = $secs / 86400;


                                  When you don't trim time part, what might lead to milscalculations. For example: Interval between 2014-05-01 14:00:00 (Y-m-d) and 2014-05-02 07:00:00 will be 0,xxx, not 1. You should trim time part of every date.



                                  So it should be:



                                  $datetime1 = strtotime(date('Y-m-d', strtotime('May 3, 2012 10:38:22 GMT')));
                                  $datetime2 = strtotime(date('Y-m-d', strtotime('06 Apr 2012 07:22:21 GMT')));

                                  $secs = $datetime2 - $datetime1;// == <seconds between the two times>
                                  $days = $secs / 86400;





                                  share|improve this answer




























                                    5














                                    Most of presented solutions seems to be working, but everyone forgets about one thing: time.



                                    Taking evan example:



                                    $datetime1 = strtotime('May 3, 2012 10:38:22 GMT');
                                    $datetime2 = strtotime('06 Apr 2012 07:22:21 GMT');

                                    $secs = $datetime2 - $datetime1;// == <seconds between the two times>
                                    $days = $secs / 86400;


                                    When you don't trim time part, what might lead to milscalculations. For example: Interval between 2014-05-01 14:00:00 (Y-m-d) and 2014-05-02 07:00:00 will be 0,xxx, not 1. You should trim time part of every date.



                                    So it should be:



                                    $datetime1 = strtotime(date('Y-m-d', strtotime('May 3, 2012 10:38:22 GMT')));
                                    $datetime2 = strtotime(date('Y-m-d', strtotime('06 Apr 2012 07:22:21 GMT')));

                                    $secs = $datetime2 - $datetime1;// == <seconds between the two times>
                                    $days = $secs / 86400;





                                    share|improve this answer


























                                      5












                                      5








                                      5







                                      Most of presented solutions seems to be working, but everyone forgets about one thing: time.



                                      Taking evan example:



                                      $datetime1 = strtotime('May 3, 2012 10:38:22 GMT');
                                      $datetime2 = strtotime('06 Apr 2012 07:22:21 GMT');

                                      $secs = $datetime2 - $datetime1;// == <seconds between the two times>
                                      $days = $secs / 86400;


                                      When you don't trim time part, what might lead to milscalculations. For example: Interval between 2014-05-01 14:00:00 (Y-m-d) and 2014-05-02 07:00:00 will be 0,xxx, not 1. You should trim time part of every date.



                                      So it should be:



                                      $datetime1 = strtotime(date('Y-m-d', strtotime('May 3, 2012 10:38:22 GMT')));
                                      $datetime2 = strtotime(date('Y-m-d', strtotime('06 Apr 2012 07:22:21 GMT')));

                                      $secs = $datetime2 - $datetime1;// == <seconds between the two times>
                                      $days = $secs / 86400;





                                      share|improve this answer













                                      Most of presented solutions seems to be working, but everyone forgets about one thing: time.



                                      Taking evan example:



                                      $datetime1 = strtotime('May 3, 2012 10:38:22 GMT');
                                      $datetime2 = strtotime('06 Apr 2012 07:22:21 GMT');

                                      $secs = $datetime2 - $datetime1;// == <seconds between the two times>
                                      $days = $secs / 86400;


                                      When you don't trim time part, what might lead to milscalculations. For example: Interval between 2014-05-01 14:00:00 (Y-m-d) and 2014-05-02 07:00:00 will be 0,xxx, not 1. You should trim time part of every date.



                                      So it should be:



                                      $datetime1 = strtotime(date('Y-m-d', strtotime('May 3, 2012 10:38:22 GMT')));
                                      $datetime2 = strtotime(date('Y-m-d', strtotime('06 Apr 2012 07:22:21 GMT')));

                                      $secs = $datetime2 - $datetime1;// == <seconds between the two times>
                                      $days = $secs / 86400;






                                      share|improve this answer












                                      share|improve this answer



                                      share|improve this answer










                                      answered May 9 '14 at 8:53









                                      ex3vex3v

                                      2,12232645




                                      2,12232645























                                          0














                                          echo 'time'.$notification_time=  "2008-12-13 10:42:00";
                                          date_default_timezone_set('Asia/Kolkata');
                                          echo 'cureen'.$currenttime=date('Y-m-d H:i:s');
                                          $now = new DateTime("$notification_time");
                                          $ref = new DateTime("$currenttime");
                                          $diff = $now->diff($ref);
                                          printf('%d days, %d hours, %d minutes', $diff->d, $diff->h, $diff->i);





                                          share|improve this answer




























                                            0














                                            echo 'time'.$notification_time=  "2008-12-13 10:42:00";
                                            date_default_timezone_set('Asia/Kolkata');
                                            echo 'cureen'.$currenttime=date('Y-m-d H:i:s');
                                            $now = new DateTime("$notification_time");
                                            $ref = new DateTime("$currenttime");
                                            $diff = $now->diff($ref);
                                            printf('%d days, %d hours, %d minutes', $diff->d, $diff->h, $diff->i);





                                            share|improve this answer


























                                              0












                                              0








                                              0







                                              echo 'time'.$notification_time=  "2008-12-13 10:42:00";
                                              date_default_timezone_set('Asia/Kolkata');
                                              echo 'cureen'.$currenttime=date('Y-m-d H:i:s');
                                              $now = new DateTime("$notification_time");
                                              $ref = new DateTime("$currenttime");
                                              $diff = $now->diff($ref);
                                              printf('%d days, %d hours, %d minutes', $diff->d, $diff->h, $diff->i);





                                              share|improve this answer













                                              echo 'time'.$notification_time=  "2008-12-13 10:42:00";
                                              date_default_timezone_set('Asia/Kolkata');
                                              echo 'cureen'.$currenttime=date('Y-m-d H:i:s');
                                              $now = new DateTime("$notification_time");
                                              $ref = new DateTime("$currenttime");
                                              $diff = $now->diff($ref);
                                              printf('%d days, %d hours, %d minutes', $diff->d, $diff->h, $diff->i);






                                              share|improve this answer












                                              share|improve this answer



                                              share|improve this answer










                                              answered Oct 22 '16 at 6:18









                                              naveen kumarnaveen kumar

                                              314214




                                              314214























                                                  0














                                                  If you want to use diff(it returns a Dateinterval object) method, the correct way is to format with %a. I mean:



                                                  If you check http://php.net/manual/en/dateinterval.format.php



                                                  The correct way is:



                                                   echo $date1->diff($date2)->format("%a");


                                                  For getting all days






                                                  share|improve this answer


























                                                  • a method name was omitted, correct is: echo $date1->diff($date2)->format("%a");

                                                    – Petr Sobotka
                                                    Jul 14 '17 at 20:54













                                                  • you are true, I will edit my response. Thanks

                                                    – Daniel Nieto
                                                    Jul 19 '17 at 10:20
















                                                  0














                                                  If you want to use diff(it returns a Dateinterval object) method, the correct way is to format with %a. I mean:



                                                  If you check http://php.net/manual/en/dateinterval.format.php



                                                  The correct way is:



                                                   echo $date1->diff($date2)->format("%a");


                                                  For getting all days






                                                  share|improve this answer


























                                                  • a method name was omitted, correct is: echo $date1->diff($date2)->format("%a");

                                                    – Petr Sobotka
                                                    Jul 14 '17 at 20:54













                                                  • you are true, I will edit my response. Thanks

                                                    – Daniel Nieto
                                                    Jul 19 '17 at 10:20














                                                  0












                                                  0








                                                  0







                                                  If you want to use diff(it returns a Dateinterval object) method, the correct way is to format with %a. I mean:



                                                  If you check http://php.net/manual/en/dateinterval.format.php



                                                  The correct way is:



                                                   echo $date1->diff($date2)->format("%a");


                                                  For getting all days






                                                  share|improve this answer















                                                  If you want to use diff(it returns a Dateinterval object) method, the correct way is to format with %a. I mean:



                                                  If you check http://php.net/manual/en/dateinterval.format.php



                                                  The correct way is:



                                                   echo $date1->diff($date2)->format("%a");


                                                  For getting all days







                                                  share|improve this answer














                                                  share|improve this answer



                                                  share|improve this answer








                                                  edited Jul 19 '17 at 10:21

























                                                  answered Dec 30 '16 at 11:20









                                                  Daniel NietoDaniel Nieto

                                                  564




                                                  564













                                                  • a method name was omitted, correct is: echo $date1->diff($date2)->format("%a");

                                                    – Petr Sobotka
                                                    Jul 14 '17 at 20:54













                                                  • you are true, I will edit my response. Thanks

                                                    – Daniel Nieto
                                                    Jul 19 '17 at 10:20



















                                                  • a method name was omitted, correct is: echo $date1->diff($date2)->format("%a");

                                                    – Petr Sobotka
                                                    Jul 14 '17 at 20:54













                                                  • you are true, I will edit my response. Thanks

                                                    – Daniel Nieto
                                                    Jul 19 '17 at 10:20

















                                                  a method name was omitted, correct is: echo $date1->diff($date2)->format("%a");

                                                  – Petr Sobotka
                                                  Jul 14 '17 at 20:54







                                                  a method name was omitted, correct is: echo $date1->diff($date2)->format("%a");

                                                  – Petr Sobotka
                                                  Jul 14 '17 at 20:54















                                                  you are true, I will edit my response. Thanks

                                                  – Daniel Nieto
                                                  Jul 19 '17 at 10:20





                                                  you are true, I will edit my response. Thanks

                                                  – Daniel Nieto
                                                  Jul 19 '17 at 10:20


















                                                  draft saved

                                                  draft discarded




















































                                                  Thanks for contributing an answer to Stack Overflow!


                                                  • Please be sure to answer the question. Provide details and share your research!

                                                  But avoid



                                                  • Asking for help, clarification, or responding to other answers.

                                                  • Making statements based on opinion; back them up with references or personal experience.


                                                  To learn more, see our tips on writing great answers.




                                                  draft saved


                                                  draft discarded














                                                  StackExchange.ready(
                                                  function () {
                                                  StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f10469037%2fsubtracting-two-dates-in-php%23new-answer', 'question_page');
                                                  }
                                                  );

                                                  Post as a guest















                                                  Required, but never shown





















































                                                  Required, but never shown














                                                  Required, but never shown












                                                  Required, but never shown







                                                  Required, but never shown

































                                                  Required, but never shown














                                                  Required, but never shown












                                                  Required, but never shown







                                                  Required, but never shown







                                                  Popular posts from this blog

                                                  Florida Star v. B. J. F.

                                                  Danny Elfman

                                                  Lugert, Oklahoma