Hello I am trying to find the exact DateTime between two dates.

For example here are my 2 dates:

2012-02-29 07:00:00
2012-02-29 09:00:00

And I want an output of :
2012-02-29 08:00:00

How can I find the EXACT time between two dates?

I am so stumped, thanks in advance for anybodies help!

Jim

    If you want to find the median time value between the two dates, what you essentially want to do is find the time difference between the two datetimes and add half that difference to the starting date.

    Are these datetime values coming from a DBMS like MySQL? Or are you trying to do this using solely PHP code?

      I have the values stored in a database, and your reasoning makes complete sense but I am looking to do it in PHP.

        creativeink;10998273 wrote:

        I am looking to do it in PHP.

        Any particular reason why? It would probably be easier (and make more sense) to do the calculation in the SQL query itself rather than in PHP.

        Regardless, it can also be done in PHP; you'll want to check out the [man]DateTime[/man] class.

          I really appreciate your help so far,

          I have messed with it for a day or so and I am 90% there, I have everything working except I cannot divide the difference in half.

          Here is what I have:

          $GetDate = dateDiff($row1['Date'], $row2['Date']); // FIND DIFFERENCE
          $NewDate = $GetDate/2; // THIS DOES NOT WORK
          
          echo $row1['Date']."<br />".$row2['Date']."<br />";
          echo $GetDate."<br />".$NewDate."<br />\n";
          
          echo date('Y-m-d H:i:s', strtotime("+".$GetDate, strtotime($row1['Date'])) )."<br />\n";
          
          echo  date("Y-m-d H:i:s", strtotime("$row1['Date'] +1 hour")); // HACK - BUT THIS IS WHAT I WANT IN THE END
          

          Here is a page where you can see the code outputs:
          http://www.creativeink.ca/masonal/admin/reorder-faq.php?qid=3&&direction=up

          The last one is a hack, but that's essentially the final outcome I am looking for.

          I used a Date Difference script from this site:
          http://www.if-not-true-then-false.com/2010/php-calculate-real-differences-between-two-dates-or-timestamps/

          Then it outputs the "2 hours", however I cannot figure out how to divide the 2 hours in half. However after this I use strotime to add the Date difference and I get the output I am looking for.

          So basically my question now is: How do I divide time in words in half? If I have 2 hours how do I get it to 1 hour? Or if it was 12 hours have it go to 6 hours?

          Thanks again for anyones help!

            The whole "dividing the time in half" part is where I gave up looking for a quick solution myself; the DateInterval::$days property would help you quickly locate the starting day that's halfway between the two dates, but you'd probably have to do some manual comparisons to adjust the time value. A simpler alternative may be to just use [man]strtotime/man to convert the two dates to Unix timestamps, add half the difference of the two timestamps onto the first timestamp, and then convert that new timestamp back into a date.

            ... which brings me to my previous question: Why not do the calculation inside the SQL query itself?

            SELECT '2012-02-29 07:00:00' + INTERVAL (TIMESTAMPDIFF(SECOND, '2012-02-29 07:00:00', '2012-02-29 09:00:00')/2) SECOND

            returns:

            2012-02-29 08:00:00
              Write a Reply...