imgrooot Posted July 17, 2019 Share Posted July 17, 2019 So this is a simple code that finds out the difference between two dates and displays it in number of days. $date1=date_create("2013-03-15"); $date2=date_create("2013-12-12"); $diff=date_diff($date1,$date2); echo $diff->format("%R%a days"); // RESULT +272 days My first question. Is it possible to remove the + sign in the result above? Second question. Is it possible to show "months" if it's greater than 30 days? And years if the days are greater than 365? How would I do this? Quote Link to comment Share on other sites More sharing options...
maxxd Posted July 17, 2019 Share Posted July 17, 2019 There is a way to do both these things. Reading the manual is a great place to start. Quote Link to comment Share on other sites More sharing options...
ginerjm Posted July 17, 2019 Share Posted July 17, 2019 (edited) Feeling magnanimous today, and having struggled to get to the correct source in the manual I"ll give you this one. Drop The R. As for showing it in months - the format options only show whole numbers so if you have a gap of less than a month you will show 0 when formatting it. And if it is more than a month you will get a value of "months" but not the full value of months & days without doing some work on figuring out how many days were in the months that did transpire. Edited July 17, 2019 by ginerjm Quote Link to comment Share on other sites More sharing options...
ginerjm Posted July 17, 2019 Share Posted July 17, 2019 As for showing it in months - The manual does show you how but it is not immediately clear. After dropping the R, change the a to d Quote Link to comment Share on other sites More sharing options...
imgrooot Posted July 17, 2019 Author Share Posted July 17, 2019 2 hours ago, ginerjm said: As for showing it in months - The manual does show you how but it is not immediately clear. After dropping the R, change the a to d Tried your way and it didn't work. Tried it this way and it works. $diff->format("%a days"); Quote Link to comment Share on other sites More sharing options...
ginerjm Posted July 18, 2019 Share Posted July 18, 2019 Yes - that gets you days without the + sign. My last change was to show the months and leftover days, if you wanted it. $dt1 = '2009-10-11'; $dt2 = '2009-10-13'; echo "Using dates of $dt1 & $dt2 :<br>"; $interval = GetInterval($dt1, $dt2); $test = $interval->format('%m'); if ($test > 0) echo 'Interval is: '.$interval->format('%m months %d days'); else echo 'Interval is: '.$interval->format('%d days'); echo '<br><br>'; // test 2 $dt1 = '2009-08-14'; $dt2 = '2009-10-13'; echo "Using dates of $dt1 & $dt2 :<br>"; $interval = GetInterval($dt1, $dt2); $test = $interval->format('%m'); if ($test > 0) echo 'Interval is: '.$interval->format('%m months %d days'); else echo 'Interval is: '.$interval->format('%d days'); echo '<br><br>'; // test 3 $dt1 = '2009-07-11'; $dt2 = '2009-09-25'; echo "Using dates of $dt1 & $dt2 :<br>"; $interval = GetInterval($dt1, $dt2); $test = $interval->format('%m'); if ($test > 0) echo 'Interval is: '.$interval->format('%m months %d days'); else echo 'Interval is: '.$interval->format('%d days'); echo '<br><br>'; exit(); //****************************************** function GetInterval($dt1, $dt2) { $datetime1 = date_create($dt1); $datetime2 = date_create($dt2); $interval = date_diff($datetime1, $datetime2); return $interval; } Here is some code for you to try out. Quote Link to comment Share on other sites More sharing options...
Barand Posted July 18, 2019 Share Posted July 18, 2019 Note that instead of using the format method to get the months value ... $test = $interval->format('%m'); you can get it directly from the "m" property of the interval object... $test = $interval->m; Similarly, you can use the ->days property instead of ->format('%a') Quote Link to comment Share on other sites More sharing options...
ginerjm Posted July 18, 2019 Share Posted July 18, 2019 Good to know for those who regularly use the OO method of coding! Quote Link to comment Share on other sites More sharing options...
Barand Posted July 18, 2019 Share Posted July 18, 2019 Not sure what you are trying to say with that comment. How would you use date intervals without "the OO method of coding"? Quote Link to comment Share on other sites More sharing options...
ginerjm Posted July 18, 2019 Share Posted July 18, 2019 I don't often code like your example, I use the functions/methods rather than properties. Quote Link to comment Share on other sites More sharing options...
Barand Posted July 18, 2019 Share Posted July 18, 2019 But $test = $interval->format('%m'); was your example. Are you claiming that isn't OO? Quote Link to comment Share on other sites More sharing options...
ginerjm Posted July 18, 2019 Share Posted July 18, 2019 As I said - I prefer the methods, not the properties directly Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.