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. Link to comment https://forums.phpfreaks.com/topic/297631-finding-difference-between-current-date-and-future-date/ 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"); Link to comment https://forums.phpfreaks.com/topic/297631-finding-difference-between-current-date-and-future-date/#findComment-1518017 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 Link to comment https://forums.phpfreaks.com/topic/297631-finding-difference-between-current-date-and-future-date/#findComment-1518019 Share on other sites More sharing options...
cloudll Posted August 4, 2015 Author 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"); 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 Link to comment https://forums.phpfreaks.com/topic/297631-finding-difference-between-current-date-and-future-date/#findComment-1518022 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.