Note that there are some explanatory texts on larger screens.

plurals
  1. POFormatting problem with PHP DateTime Difference when used as part of an equation
    primarykey
    data
    text
    <p>I'm trying to write a simple invoicing system. I wanted to use DateTime Diff to calculate the cost of each invoice.</p> <p>So far I have:</p> <pre><code>/* In this example $dayLength and $ratePerSec don't really matter but we will use the hypotethical values of 8.0 and 0.0054 for now */ $date1 = new DateTime( "Time1FromDatabase", [DateTimeZoneOfChoice] ); $date2 = new DateTime( "Time2FromDatabase", [DateTimeZoneOfChoice] ); $diff = $date2-&gt;diff( $date1 ); $lenInSec = $diff-&gt;format("%y")*365*$dayLength*3600 + $diff-&gt;format("%d")*$dayLength*3600 + $diff-&gt;format("%h")*3600 + $diff-&gt;format("%i")*60 + $diff-&gt;format("%s"); $cost = $lenInSec * $ratePerSecond; </code></pre> <p>When I try to output cost using the $lenInSec generated from DateTime Diff I get really odd results. Sometimes, anywhere from 1 in 3 and 1 in 50 times, I get the correct value of say 460.25. A lot of the time I just get a random string of letters, numbers or symbols, such as: þ50.</p> <p>I've found that if I echo out the result 2+ times then on the final output I get the expected result perfectly.</p> <p>My temporary fix at the moment is, in the getCost() function, to run:</p> <pre><code>ob_start(); echo $cost; echo $cost; ob_clean(); return $cost; </code></pre> <p>And so far this seems to fix the problem and I seem to get the expected result every time but it's a bit weird.</p> <p>I know it's the DateTime Diff in the equation because if I replace it with the old-style:</p> <pre><code>strtotime( "Time2FromDatabase") - strtotime( "Time1FromDatabase") </code></pre> <p>it works perfectly every time.</p> <p>I also have a running total cost:</p> <pre><code>$totalCost += $cost; </code></pre> <p>Outputting $totalCost always outputs the correct value regardless of whether I use the DateTime Diff method or the strtotime method.</p> <p>I've also tried using intval. (int) and (string) casts on the $lenInSec variable before using it in the equation and it has no effect.</p> <p>Can anyone give any clues as to why this is happening? Is it a(n) (un)known bug in PHP's DateTime/Diff classes?</p> <p>EDIT:</p> <pre><code>$date1 = new DateTime( "2011-09-04 09:00:00", new DateTimeZone("UTC") ); $date2 = new DateTime( "2011-09-04 18:00:00", new DateTimeZone("UTC") ); //Essentially 1 day of work $rate = 200.00; //Per Day $ratePerSec = 0.00617283950617283950617283950617; // $rate / (9 hours * 3600 seconds ) $lenInSec = 32400; // From the diff calculation $cost = $lenInSec * $ratePerSec; // Cost should now equal 200. echo $cost; // Sometimes outputs 8, other times 200, other times þ50. //I think the þ50 is when you have a number like 425.50 where þ is the 425. $totalCost += $cost; echo number_format($totalCost,2); /* Will return the correct value of all the $cost variables added no matter what the individual $cost variables are output as, in this case 200.00 */ </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload