mysql - Formatting problem with PHP DateTime Difference when used as part of an equation -
I am trying to write a simple invoicing system. I want to use the Datetime Diff to calculate the cost of each invoice.
I have so far:
/ * In this example $ dayLength and $ ratePerSec is not really the case, but now we use the hypotethical values of 8.0 and 0.0054 Will * / $ date1 = new date time ("Time 1 Form Database", [DateTimeJonof Chase]); $ Date2 = new date time ("Time 2 Form Database", [Datetime Joneof Choice]); $ 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 * $ rate per second; When I try to output cost using $ lenInSec generated from the Deltaime Diff, I actually get weird results. Occasionally, in 1 and 3 out of 1 to 3 times, I get the right value to say 460.25. Sometimes I get a random string of letters, numbers, or symbols, such as: ¾50.
I have found that if I explain the result twice to the output twice, i get the result of the hope completely.
At this time, my temporary running fine, to getCost () function, to run:
ob_start (); Counterpart $ cost; Counterpart $ cost; Ob_clean (); Return cost $; And by now it seems to fix the problem and I get the expected result every time, but it's a little weird.
I know that this date is the time fraction equation because if I change it with the old-style:
strtotime ("Time2FromDatabase") - strtotime ("Time1FromDatabase ") It works perfectly every time.
I also have the total cost to run:
$ totalCost + = $ cost; $ Output of the total I always output to the right value regardless of whether I use the Datetime Diff method or the Straotomy method.
I also tried to use the oval (int) and put the string (string) in the $ lenInSec variable before using it in the equation and it had no effect.
Can someone point out why this is happening? Is this (N) (UN) known bug in the date / diff sections of PHAP?
Edit:
$ date1 = new date time ("2011-09-04 09: 00: 00", new datetime time zone ("UTC")); $ Date2 = new date time ("2011-09-04 18:00:00", new datetime time zone ("UTC")); // Essentially 1 day work $ rate = 200.00; // $ per day PERSec = 0.00617283950617283950617283950617; // $ rate / (9 hours * 3600 seconds) $ lenInSec = 32400; // $ cost = $ lenInSec * $ ratePerSec from differential calculation; // Cost should now be 200 equals Echo cost; // ever output 8, other times 200, other times ¾50 // I think that is ¾50 when you have a number like 425.50 where the SI is 425. $ TotalCost + = $ cost; Echo number-format ($ total, 2); / * Will return the correct value of all $ cost variables, it does not matter if $ 200 * * $ /
As far as I can tell, this date is not a bug in the time category.
Working with the code snippets you provided and your comments, I created it:
$ total cost = 0; // Unsupported in your sample code, potential source of problem $ dayLength = 9; $ Rate = 425.50; $ At $ rate = $ rate / $ ($ 3700); // 2 days, 3 hours $ date1 = new date time ("2011-09-10 09:00", new datetime time zone ("UTC")); $ Date2 = new date time ("2011-11-10 12:00:00", new datetime time zone ("UTC")); $ 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 * $ rate per second; $ Total cost + = $ cost; Echo number-format ($ total, 2); Which always yields 141.83 . You are not actually providing enough code or the concrete example generates a formatting problem. Any code, numbers, or symbols in the code provided by you should not be the result of the random string.
Comments
Post a Comment