Check if given time minus now time is less than 60 minutes using php












1















I have read a lot here, and tried a lot, but I didn't get it :(



I have:



$end = new DateTime('22:00');
$now = new DateTime();

if(($end - $now) < 60)
{
$bijnaTijd = "bijnaTijdJa";
}


I want to see if the difference between the $end and the current time is less than 60 minutes. Can anybody help me out?










share|improve this question





























    1















    I have read a lot here, and tried a lot, but I didn't get it :(



    I have:



    $end = new DateTime('22:00');
    $now = new DateTime();

    if(($end - $now) < 60)
    {
    $bijnaTijd = "bijnaTijdJa";
    }


    I want to see if the difference between the $end and the current time is less than 60 minutes. Can anybody help me out?










    share|improve this question



























      1












      1








      1


      0






      I have read a lot here, and tried a lot, but I didn't get it :(



      I have:



      $end = new DateTime('22:00');
      $now = new DateTime();

      if(($end - $now) < 60)
      {
      $bijnaTijd = "bijnaTijdJa";
      }


      I want to see if the difference between the $end and the current time is less than 60 minutes. Can anybody help me out?










      share|improve this question
















      I have read a lot here, and tried a lot, but I didn't get it :(



      I have:



      $end = new DateTime('22:00');
      $now = new DateTime();

      if(($end - $now) < 60)
      {
      $bijnaTijd = "bijnaTijdJa";
      }


      I want to see if the difference between the $end and the current time is less than 60 minutes. Can anybody help me out?







      php datetime time






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 25 '18 at 4:51









      Mohammad

      15.7k123564




      15.7k123564










      asked Nov 24 '18 at 20:55









      franky555franky555

      104




      104
























          2 Answers
          2






          active

          oldest

          votes


















          0














          You need to get timestamp from DateTime object using getTimestamp() and compare timestamps with each other.



          Note that timestamp is in second so i used 60*60 to converting minutes to seconds.



          $end = new DateTime('22:00');
          $now = new DateTime();

          if (($end->getTimestamp() - $now->getTimestamp()) < 60*60){
          // do something
          }


          Also you can dates function instead of DateTime class.



          if (strtotime('22:00') - time() < 60*60){
          // do something
          }





          share|improve this answer































            0














            please check this code , hope it solve your problem



            <?php 
            $end = new DateTime('22:00');
            $new_end = strtotime($end->format('Hi')); //convert to string
            $end_h = idate('h', $new_end); //get the hour
            $end_h = str_pad($end_h, 2, 0, STR_PAD_RIGHT);
            $end_m = idate('i', $new_end);
            $end_m = str_pad($end_m, 2, 0, STR_PAD_LEFT); //get leading zero for above minutes 10
            $time_end = $end_h.''.$end_m;

            $now = new DateTime();
            $new_now = strtotime($now->format('Hi'));
            $now_h = idate('h', $new_now);
            $now_h = str_pad($now_h, 2, 0, STR_PAD_RIGHT);
            $now_m = idate('i', $new_now);
            $now_m = str_pad($now_m, 2, 0, STR_PAD_LEFT);
            $time_now = $now_h.''.$now_m;


            echo ($time_end - $time_now); //get the result do your logic after this code





            share|improve this answer























              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%2f53462285%2fcheck-if-given-time-minus-now-time-is-less-than-60-minutes-using-php%23new-answer', 'question_page');
              }
              );

              Post as a guest















              Required, but never shown

























              2 Answers
              2






              active

              oldest

              votes








              2 Answers
              2






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes









              0














              You need to get timestamp from DateTime object using getTimestamp() and compare timestamps with each other.



              Note that timestamp is in second so i used 60*60 to converting minutes to seconds.



              $end = new DateTime('22:00');
              $now = new DateTime();

              if (($end->getTimestamp() - $now->getTimestamp()) < 60*60){
              // do something
              }


              Also you can dates function instead of DateTime class.



              if (strtotime('22:00') - time() < 60*60){
              // do something
              }





              share|improve this answer




























                0














                You need to get timestamp from DateTime object using getTimestamp() and compare timestamps with each other.



                Note that timestamp is in second so i used 60*60 to converting minutes to seconds.



                $end = new DateTime('22:00');
                $now = new DateTime();

                if (($end->getTimestamp() - $now->getTimestamp()) < 60*60){
                // do something
                }


                Also you can dates function instead of DateTime class.



                if (strtotime('22:00') - time() < 60*60){
                // do something
                }





                share|improve this answer


























                  0












                  0








                  0







                  You need to get timestamp from DateTime object using getTimestamp() and compare timestamps with each other.



                  Note that timestamp is in second so i used 60*60 to converting minutes to seconds.



                  $end = new DateTime('22:00');
                  $now = new DateTime();

                  if (($end->getTimestamp() - $now->getTimestamp()) < 60*60){
                  // do something
                  }


                  Also you can dates function instead of DateTime class.



                  if (strtotime('22:00') - time() < 60*60){
                  // do something
                  }





                  share|improve this answer













                  You need to get timestamp from DateTime object using getTimestamp() and compare timestamps with each other.



                  Note that timestamp is in second so i used 60*60 to converting minutes to seconds.



                  $end = new DateTime('22:00');
                  $now = new DateTime();

                  if (($end->getTimestamp() - $now->getTimestamp()) < 60*60){
                  // do something
                  }


                  Also you can dates function instead of DateTime class.



                  if (strtotime('22:00') - time() < 60*60){
                  // do something
                  }






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 25 '18 at 4:49









                  MohammadMohammad

                  15.7k123564




                  15.7k123564

























                      0














                      please check this code , hope it solve your problem



                      <?php 
                      $end = new DateTime('22:00');
                      $new_end = strtotime($end->format('Hi')); //convert to string
                      $end_h = idate('h', $new_end); //get the hour
                      $end_h = str_pad($end_h, 2, 0, STR_PAD_RIGHT);
                      $end_m = idate('i', $new_end);
                      $end_m = str_pad($end_m, 2, 0, STR_PAD_LEFT); //get leading zero for above minutes 10
                      $time_end = $end_h.''.$end_m;

                      $now = new DateTime();
                      $new_now = strtotime($now->format('Hi'));
                      $now_h = idate('h', $new_now);
                      $now_h = str_pad($now_h, 2, 0, STR_PAD_RIGHT);
                      $now_m = idate('i', $new_now);
                      $now_m = str_pad($now_m, 2, 0, STR_PAD_LEFT);
                      $time_now = $now_h.''.$now_m;


                      echo ($time_end - $time_now); //get the result do your logic after this code





                      share|improve this answer




























                        0














                        please check this code , hope it solve your problem



                        <?php 
                        $end = new DateTime('22:00');
                        $new_end = strtotime($end->format('Hi')); //convert to string
                        $end_h = idate('h', $new_end); //get the hour
                        $end_h = str_pad($end_h, 2, 0, STR_PAD_RIGHT);
                        $end_m = idate('i', $new_end);
                        $end_m = str_pad($end_m, 2, 0, STR_PAD_LEFT); //get leading zero for above minutes 10
                        $time_end = $end_h.''.$end_m;

                        $now = new DateTime();
                        $new_now = strtotime($now->format('Hi'));
                        $now_h = idate('h', $new_now);
                        $now_h = str_pad($now_h, 2, 0, STR_PAD_RIGHT);
                        $now_m = idate('i', $new_now);
                        $now_m = str_pad($now_m, 2, 0, STR_PAD_LEFT);
                        $time_now = $now_h.''.$now_m;


                        echo ($time_end - $time_now); //get the result do your logic after this code





                        share|improve this answer


























                          0












                          0








                          0







                          please check this code , hope it solve your problem



                          <?php 
                          $end = new DateTime('22:00');
                          $new_end = strtotime($end->format('Hi')); //convert to string
                          $end_h = idate('h', $new_end); //get the hour
                          $end_h = str_pad($end_h, 2, 0, STR_PAD_RIGHT);
                          $end_m = idate('i', $new_end);
                          $end_m = str_pad($end_m, 2, 0, STR_PAD_LEFT); //get leading zero for above minutes 10
                          $time_end = $end_h.''.$end_m;

                          $now = new DateTime();
                          $new_now = strtotime($now->format('Hi'));
                          $now_h = idate('h', $new_now);
                          $now_h = str_pad($now_h, 2, 0, STR_PAD_RIGHT);
                          $now_m = idate('i', $new_now);
                          $now_m = str_pad($now_m, 2, 0, STR_PAD_LEFT);
                          $time_now = $now_h.''.$now_m;


                          echo ($time_end - $time_now); //get the result do your logic after this code





                          share|improve this answer













                          please check this code , hope it solve your problem



                          <?php 
                          $end = new DateTime('22:00');
                          $new_end = strtotime($end->format('Hi')); //convert to string
                          $end_h = idate('h', $new_end); //get the hour
                          $end_h = str_pad($end_h, 2, 0, STR_PAD_RIGHT);
                          $end_m = idate('i', $new_end);
                          $end_m = str_pad($end_m, 2, 0, STR_PAD_LEFT); //get leading zero for above minutes 10
                          $time_end = $end_h.''.$end_m;

                          $now = new DateTime();
                          $new_now = strtotime($now->format('Hi'));
                          $now_h = idate('h', $new_now);
                          $now_h = str_pad($now_h, 2, 0, STR_PAD_RIGHT);
                          $now_m = idate('i', $new_now);
                          $now_m = str_pad($now_m, 2, 0, STR_PAD_LEFT);
                          $time_now = $now_h.''.$now_m;


                          echo ($time_end - $time_now); //get the result do your logic after this code






                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered Nov 24 '18 at 21:34









                          rheeantzrheeantz

                          709412




                          709412






























                              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%2f53462285%2fcheck-if-given-time-minus-now-time-is-less-than-60-minutes-using-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

                              Create new schema in PostgreSQL using DBeaver

                              Deepest pit of an array with Javascript: test on Codility

                              Costa Masnaga