-1

I've got the following code, but this gives me whole numbers:

<?php
   $datetime1 = new DateTime('2009-10-11 00:00:00');
   $datetime2 = new DateTime('2009-10-11 00:00:10');
   $interval = $datetime1->diff($datetime2);
   echo $interval->format('%R%a days');
?>

How do I count days and show the decimal points to? In the example above, there is a difference of 10 seconds, how do I get it to display 0.000000something days?

mawburn
  • 2,232
  • 4
  • 29
  • 48
oshirowanen
  • 15,297
  • 82
  • 198
  • 350
  • check this http://stackoverflow.com/questions/2040560/how-to-find-number-of-days-between-two-dates-using-php – Charaf JRA Sep 06 '13 at 20:21
  • `diff()` will only ever return the difference in days. Your two dates have the same days, so there'll be no difference. You'd want to convert to a plain "number of seconds" difference instead, and format that into an hh:mm:ss type string. – Marc B Sep 06 '13 at 20:21
  • Just divide the difference in seconds by the number of seconds in a day. – Alex Howansky Sep 06 '13 at 20:21
  • I know examples exist of seconds. I am not after that. I am after days to the decimal point. i.e. if there is a difference of 24 hours, I want to see 0. days. If there is a difference of 1 and a half days, I want to see 1.5 days. If there is a difference of 1 day and 3/4 of a day, I want to see 1.75 days etc. – oshirowanen Sep 06 '13 at 20:31

1 Answers1

1

Why not use strtotime?

$oneDay = 24 * 60 * 60;
$datetime1 = strtotime('2009-10-11 00:00:00');
$datetime2 = strtotime('2009-10-11 00:00:10');
$interval = $datetime2 - $datetime1; // seconds
$days = $interval / $oneDay; // fractions of days
Sergiu Paraschiv
  • 9,929
  • 5
  • 36
  • 47