4

I'm comparing dates with this code:

$date1 = new DateTime("2007-03-24 12:10:00"); $date2 = new DateTime("2009-06-26 14:00:30"); $interval = $date1->diff($date2); 

If I echo this: echo $interval->m." months and".$interval->d." days."; I get the output 3 months and 2 days.. Now, I want to echo the difference between the dates but include the amount of months in the day count, so a difference of 1 month (with 30 days in it) and 5 days would be 35 days, not 1 month and 5 days. How do I do this?

I'm using PHP version 5.3+.

3
  • You can use the format function Commented Apr 22, 2013 at 15:06
  • @Kacey no, there are better ways Commented Apr 22, 2013 at 15:07
  • @STTLCU Thanks. I didn't really think about what he wanted before posting. Commented Apr 22, 2013 at 15:15

2 Answers 2

5

You should be able to use:

$interval->days; 

See: http://www.php.net/manual/en/class.dateinterval.php#dateinterval.props.days

echo "There are ".$interval->days." days between the two dates."; 
Sign up to request clarification or add additional context in comments.

2 Comments

Well...that was easier then expected! Accepting in 9 minutes (damn timer..).
Quick offtopic: how do I make a datetime object from the current date and time?
1

Your $interval variable is of type DateInterval.

Therefore, $interval->days should yeld the desired output.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.