Basic integer division in PHP is failing - Am clueless
(Looks like something's wrong with my environment / system. Am analyzing it currently. Every logical answer was tried and it failed. So, will report back once I have more to share. Thanks for the answers!)
I have written some simple PHP code to calculate the duration between two dates, and do some basic arithmetic, to calculate some percentage value.
I am at a loss of clues on why this is not working! Seems to me that a variable is treated as an integer on one line and a string on another.
$start_DT = 开发者_Python百科new DateTime($startdate); // e.g. 2011-06-07
$end_DT = new DateTime($enddate); // e.g. 2011-06-14
$today_DT = new DateTime("now"); // 2011-06-09
$duration = date_diff($end_DT, $start_DT)->d;
$days_remaining = date_diff($end_DT, $today_DT)->d;
echo $days_remaining; // This outputs a value of "4" in my specific case
echo $duration; // This outputs a value of "7" for my specific case.
$percentage_dur_complete = $days_remaining / $duration;
echo $percentage_dur_complete; // This gives a value of NAN
// This line says that I am dividing my zero, to imply that
// $duration might be a string.
$percentage_dur_complete = $days_remaining / (float) $duration;
Am I missing something basic? I am a relative newbie (2 months) to PHP. I really hope (with the risk of appearing stupid) that there's something I've missed out.
Thanks!
try
$percentage_dur_complete = (int)$days_remaining / (int)$duration;
EDIT: This works for me...
<?php
$startdate = '2011-06-05';
$enddate = '2011-06-12';
$today_DT = new DateTime("now");
$start_DT = new DateTime($startdate); // e.g. 2011-06-07
$end_DT = new DateTime($enddate);
$duration = date_diff($end_DT, $start_DT)->d;
$days_remaining = date_diff($end_DT, $today_DT)->d;
var_dump ($duration);
var_dump ($days_remaining);
$percentage_dur_complete = $days_remaining / $duration *100;
echo ($percentage_dur_complete);
?>
If it doesnt for you, it is most definitely an issue with your PHP installation/version!
I took your code and modified it a bit (formatted output, but more importantly passed direct strings into the DateTime
functions):
$start_DT = new DateTime('2011-06-07');
$end_DT = new DateTime('2011-06-14');
$today_DT = new DateTime("now");
$duration = date_diff($end_DT, $start_DT)->d;
$days_remaining = date_diff($end_DT, $today_DT)->d;
echo $days_remaining . "<br>";
echo $duration . "<br>";
$percentage_dur_complete = $days_remaining / $duration;
echo $percentage_dur_complete . "<br>";
$percentage_dur_complete = $days_remaining / (float) $duration;
echo $percentage_dur_complete . "<br>";
And I'm getting output that looks like this:
5
7
0.71428571428571
0.71428571428571
My hunch is that you're not passing what you think you're passing to generate $start_DT
and $end_DT
.
I'll bet that DateTime's constructor is having trouble with your incoming dates. Try this:
$startdate = "2011-06-07";
$enddate = "2011-06-14";
$start_DT = DateTime::createFromFormat('Y-m-j',$startdate);
$end_DT = DateTime::createFromFormat('Y-m-j',$enddate);
$today_DT = new DateTime("now");
$duration = date_diff($end_DT, $start_DT)->d;
$days_remaining = date_diff($end_DT, $today_DT)->d;
echo $days_remaining . "<br>\n";
echo $duration . "<br>\n";
$percentage_dur_complete = $days_remaining / $duration;
echo $percentage_dur_complete . "<br>\n";
$percentage_dur_complete = $days_remaining / (float) $duration;
echo $percentage_dur_complete . "<br>\n";
Make sure the format string matches how you're passing in your date strings (i.e. 'j' for days with leading zero, 'd' for days without).
I had the same problem. I was using PHP 5.3.1 and I upgraded to 5.4.17 to solve the issue. Don't waste time trying to find a workaround like I did initially.
精彩评论