6

(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 = 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!

8
  • date_diff returns a DateInterval, not an integer. Commented Jun 9, 2011 at 21:57
  • 1
    One way to check what type each variable is before the division is to use var_dump( $myvar ), which will tell you the type and value of $myvar. More info here: php.net/manual/en/function.var-dump.php Commented Jun 9, 2011 at 21:59
  • Yes, I figured that out. I am accessing its member variable for days (->d) to get the actual integer within it. Commented Jun 9, 2011 at 22:01
  • If I do var_dump, then both numbers are integers and $percentage_dur_complete contains a proper number. There must be something else wrong. Are you sure $startdate and $enddate are correctly set? Commented Jun 9, 2011 at 22:02
  • @Paul Sonier - Maybe the OP knows that? Maybe that's why the OP selects the $d member of the object? Maybe you should look at all of the little squigglies before putting people down? And maybe everybody else should too before upvoting a snide and wrong remark? Commented Jun 9, 2011 at 22:04

4 Answers 4

2

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!

Sign up to request clarification or add additional context in comments.

2 Comments

Sleep doesn't work, although I wonder why it makes sense to add? Seems a bit unrelated?
Yes, its likely something with my system that's wonky. Thanks for the response. Will have to figure out what's going on at my end
0

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.

13 Comments

I tried this out. What's wierd(er) is that every OTHER operation with $duration fails, and every OTHER works. So, if I add an extra echo for $duration, my own code works OK. Can't figure out why
I'm not sure what you mean? You mean that it alternates between working and not working?
So, if I copy paste your code as-is, this is what I get: 3 7 NAN 0.42857142857143
Yes, every alternate operation with $duration works. I know it sounds silly. :(
What locale are you in? Maybe the DateTime constructor is not parsing the date correctly because it's looking for another format.
|
0

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).

1 Comment

Thanks. This worked for a bit, and then stopped working. I think something is wrong in my install. Will reinstall everything / try it on a different system. Even if I take your code as is, it works intermittently. Makes me believe that its my system
0

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.

1 Comment

is your answer really related to date_diff issue that is exposed in the question?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.