Steve_NI Posted November 24, 2013 Share Posted November 24, 2013 I am trying to work the date difference between two dates I have a form that a user completes and in that form they provide an end date. In my php I convert this to a unix date: $stamp = strtotime($_POST['endldate']);$endDate = date('Y-m-d', $stamp); I then want todays date: $today = date('Y-m-d'); I then want to get the difference in days between the two. I have tried $difference = datediff($endDate,$today) But this returns an error = date_diff() expects parameter 1 to be DateTime What i want to do is then divide the difference by 7 to get the number of weeks How do I get around the error? Quote Link to comment https://forums.phpfreaks.com/topic/284220-date-differences/ Share on other sites More sharing options...
Ch0cu3r Posted November 24, 2013 Share Posted November 24, 2013 (edited) You need to initialise your dates with the DateTime object before you use date_diff() See examples herehttp://www.php.net/manual/en/datetime.diff.php Edited November 24, 2013 by Ch0cu3r Quote Link to comment https://forums.phpfreaks.com/topic/284220-date-differences/#findComment-1459833 Share on other sites More sharing options...
Steve_NI Posted November 24, 2013 Author Share Posted November 24, 2013 You need to initialise your dates with the DateTime object before you use date_diff() See examples here http://www.php.net/manual/en/datetime.diff.php I followed the examples with my code: $start = date_create($goalDate); $end = date_create($today); $interval = date_diff($goalDate,$today); I still get the same error as before : date_diff() expects parameter 1 to be DateTime The examples seem to use hard coded dates, but mine will depend on what the user inserts and also will differ dependent upon the current date. Quote Link to comment https://forums.phpfreaks.com/topic/284220-date-differences/#findComment-1459837 Share on other sites More sharing options...
objnoob Posted November 24, 2013 Share Posted November 24, 2013 (edited) $goalDate is the string.. $start is the DateTime object So $interval = date_diff($start,$end); Edited November 24, 2013 by objnoob Quote Link to comment https://forums.phpfreaks.com/topic/284220-date-differences/#findComment-1459841 Share on other sites More sharing options...
Steve_NI Posted November 24, 2013 Author Share Posted November 24, 2013 $goalDate is the string.. $start is the DateTime object So $interval = date_diff($start,$end); Thanks for your help When I try: $today = date('Y-m-d'); $start = date_create($goalDate); $interval = date_diff($start,$today); I get the error : date_diff() expects parameter 2 to be DateTime, string given But when I then make the second parameter a string: $start = date_create($goalDate); $end = date_create($today); $interval = date_diff($start,$end); I get the error: Object of class DateInterval could not be converted to string I'm getting myself very confused all I want is a number of days between the two dates that I could divide by 7 to work out the number of weeks between the them Quote Link to comment https://forums.phpfreaks.com/topic/284220-date-differences/#findComment-1459845 Share on other sites More sharing options...
objnoob Posted November 24, 2013 Share Posted November 24, 2013 $objToday = new DateTime(date('Y-m-d')); # create datetime object for today $objStart = new DateTime($goalDate); # create datetime object for goal date $objDuration = $objStart->diff($objToday, true); # create a DateInterval object using the diff method of our $objStart datetime object passing in $objToday. echo 'Days since start date: ' . $objDuration->format('d'); # call the format method of our DateInterval object to say we just want the number of FULL (whole) DAYS since the start Quote Link to comment https://forums.phpfreaks.com/topic/284220-date-differences/#findComment-1459846 Share on other sites More sharing options...
Barand Posted November 24, 2013 Share Posted November 24, 2013 (edited) For the interval format you need format('%a') eg $goalDate = '2013-06-20'; $objToday = new DateTime(); $objStart = new DateTime($goalDate); $objDuration = $objStart->diff($objToday, true); echo 'Days since start date: ' . $objDuration->format('d'); // --> d echo 'Days since start date: ' . $objDuration->format('%d'); // --> 4 (days remaining after months calculated) echo 'Days since start date: ' . $objDuration->format('%a'); // --> 157 (total days) // or echo 'Days since start date: ' . $objDuration->days; // --> 157 Edited November 24, 2013 by Barand Quote Link to comment https://forums.phpfreaks.com/topic/284220-date-differences/#findComment-1459859 Share on other sites More sharing options...
Steve_NI Posted November 24, 2013 Author Share Posted November 24, 2013 $objToday = new DateTime(date('Y-m-d')); # create datetime object for today $objStart = new DateTime($goalDate); # create datetime object for goal date $objDuration = $objStart->diff($objToday, true); # create a DateInterval object using the diff method of our $objStart datetime object passing in $objToday. echo 'Days since start date: ' . $objDuration->format('d'); # call the format method of our DateInterval object to say we just want the number of FULL (whole) DAYS since the start Appreciate your efforts I tried that but it printed out Days since start date d ! Quote Link to comment https://forums.phpfreaks.com/topic/284220-date-differences/#findComment-1459860 Share on other sites More sharing options...
objnoob Posted November 24, 2013 Share Posted November 24, 2013 (edited) ooops change For the interval format you need format('%a') eg $goalDate = '2013-06-20'; $objToday = new DateTime(); $objStart = new DateTime($goalDate); $objDuration = $objStart->diff($objToday, true); echo 'Days since start date: ' . $objDuration->format('d'); // --> d echo 'Days since start date: ' . $objDuration->format('%d'); // --> 4 (days remaining after months calculated) echo 'Days since start date: ' . $objDuration->format('%a'); // --> 157 (total days) // or echo 'Days since start date: ' . $objDuration->days; // --> 157 Thanks Use %a , I mean use $objDuration->days; lol Edited November 24, 2013 by objnoob Quote Link to comment https://forums.phpfreaks.com/topic/284220-date-differences/#findComment-1459861 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.