cloudll Posted August 4, 2015 Share Posted August 4, 2015 Hey, I have been trying to tweak this code to tell me the days between the current date and a future date. The snippet below however sets a start date rather than using the current date. I have been trying to format date() in a way that will work but have had no luck. $date1=date_create("2013-03-15"); $date2=date_create("2013-12-12"); $diff=date_diff($date1,$date2); echo $diff->format("%R%a days"); Could someone tell me what I need to do to get it working please? Thank you. Quote Link to comment Share on other sites More sharing options...
gizmola Posted August 4, 2015 Share Posted August 4, 2015 Use the datetime object, and this is pretty simple. $date1 = new DateTime(); $date2 = date_create("2013-12-12"); $diff = date_diff($date1,$date2, true); echo $diff->format("%R%a days"); Quote Link to comment Share on other sites More sharing options...
Barand Posted August 4, 2015 Share Posted August 4, 2015 or, wholly DateTime $date1 = new DateTime("2013-03-15"); $date2 = new DateTime("2013-12-12"); echo $date1->diff($date2)->days; // 272 1 Quote Link to comment Share on other sites More sharing options...
cloudll Posted August 4, 2015 Author Share Posted August 4, 2015 (edited) Use the datetime object, and this is pretty simple. $date1 = new DateTime(); $date2 = date_create("2013-12-12"); $diff = date_diff($date1,$date2, true); echo $diff->format("%R%a days"); This works well to work out the difference Thanks. However I cant seem to echo $date1. Is this normal with datetime? EDIT: Figured it out, my silly mistake Edited August 4, 2015 by cloudll 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.