Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. Linux, Apache, MySQL, PHP
  4. how to get date difference

how to get date difference

Scheduled Pinned Locked Moved Linux, Apache, MySQL, PHP
phphelptutorialquestion
7 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • U Offline
    U Offline
    User 9106259
    wrote on last edited by
    #1

    hi i have 4 fields like as bellow 1.start date 2.start time 3.end date 4.end time by using above fields how can i get difference in hours in php project any body can help me. thank you.

    P D A M 4 Replies Last reply
    0
    • U User 9106259

      hi i have 4 fields like as bellow 1.start date 2.start time 3.end date 4.end time by using above fields how can i get difference in hours in php project any body can help me. thank you.

      P Offline
      P Offline
      Peter_in_2780
      wrote on last edited by
      #2

      A little RTFM goes a long way. Open up the PHP manual (online if you were too stupid or lazy to download a free copy) and type "date difference" into the search box. Work with DateTime and DateInterval objects. Peter

      Software rusts. Simon Stephenson, ca 1994. So does this signature. me, 2012

      U 1 Reply Last reply
      0
      • P Peter_in_2780

        A little RTFM goes a long way. Open up the PHP manual (online if you were too stupid or lazy to download a free copy) and type "date difference" into the search box. Work with DateTime and DateInterval objects. Peter

        Software rusts. Simon Stephenson, ca 1994. So does this signature. me, 2012

        U Offline
        U Offline
        User 9106259
        wrote on last edited by
        #3

        that is not happening so that only posted.

        P 1 Reply Last reply
        0
        • U User 9106259

          that is not happening so that only posted.

          P Offline
          P Offline
          Peter_in_2780
          wrote on last edited by
          #4

          ?????? Try this link[^] then.

          Software rusts. Simon Stephenson, ca 1994. So does this signature. me, 2012

          1 Reply Last reply
          0
          • U User 9106259

            hi i have 4 fields like as bellow 1.start date 2.start time 3.end date 4.end time by using above fields how can i get difference in hours in php project any body can help me. thank you.

            D Offline
            D Offline
            Deepthi 21456
            wrote on last edited by
            #5

            <?php
            $date1 = "2012-10-04 22:45:00";
            $date2 = "2013-12-05 13:44:01";
            $diff = abs(strtotime($date2) - strtotime($date1));
            $years = floor($diff / (365*60*60*24));
            $months = floor(($diff - $years * 365*60*60*24) / (30*60*60*24));
            $days = floor(($diff - $years * 365*60*60*24 - $months*30*60*60*24)/ (60*60*24));
            $hours = floor(($diff - $years * 365*60*60*24 - $months*30*60*60*24 - $days*60*60*24)/ (60*60));
            $minuts = floor(($diff - $years * 365*60*60*24 - $months*30*60*60*24 - $days*60*60*24 - $hours*60*60)/ 60);
            $seconds = floor(($diff - $years * 365*60*60*24 - $months*30*60*60*24 - $days*60*60*24 - $hours*60*60 - $minuts*60));
            printf("%d years, %d months, %d days, %d hours, %d minuts\n, %d seconds\n", $years, $months, $days, $hours, $minuts, $seconds);
            die;
            ?>

            1 Reply Last reply
            0
            • U User 9106259

              hi i have 4 fields like as bellow 1.start date 2.start time 3.end date 4.end time by using above fields how can i get difference in hours in php project any body can help me. thank you.

              A Offline
              A Offline
              Anilannu
              wrote on last edited by
              #6

              What is your project... how you help me if i help u

              @n!L

              1 Reply Last reply
              0
              • U User 9106259

                hi i have 4 fields like as bellow 1.start date 2.start time 3.end date 4.end time by using above fields how can i get difference in hours in php project any body can help me. thank you.

                M Offline
                M Offline
                Mohibur Rashid
                wrote on last edited by
                #7

                If you searched on google you would find 1000's of solutions. this is copied from PHP site

                <?php
                $td = time_diff($timestamp1-$timestamp2); // has to be a positive result
                $td .= ($td=="now")? "":" ago"; // in this example "ago"
                echo $td;

                function time_diff($s){

                $time = get\_post\_time('G', true, $post);
                $s = time() - $time;
                
                if($s>=1) {
                $td = "$s sec";
                }  
                
                if($s>59) {
                    $m = (int)($s/60);
                    $s = $s-($m\*60); // sec left over
                    $td = "$m min"; if($s>1) $td .="s";
                }
                if($m>59){
                    $hr = (int)($m/60);
                    $m = $m-($hr\*60); // min left over
                    $td = "$hr hr"; if($hr>1) $td .= "s";
                    if($m>0) $td .= ", $m min";
                }
                if($hr>23){
                    $d = (int)($hr/24);
                    $hr = $hr-($d\*24); // hr left over
                    $td = "$d day"; if($d>1) $td .= "s";
                    if($d<3){
                        if($hr>0) $td .= ", $hr hr"; if($hr>1) $td .= "s";
                    }
                }
                return $td;
                

                }
                ?>

                I wish I could believe there is an after life.

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups