PHP date_sub. can't substract today and date -
i trying output number of days between today , date enter have problem encounter error: "warning: date_diff() expects parameter 2 datetimeinterface" what's problem ?
<?php $today=date("y-m-d"); $date=date_create("2016-09-16"); echo date_diff($date,$today); ?>
your problem lies in when using date_diff
have make sure you're comparing objects actual date objects. return type date_diff
dateinterval object. you're treating string.
$today = new datetime(); // $today datetime object $date = new datetime("2016-09-16"); // $date datetime object! $diff = date_diff($date,$today); // compare 2 objects of same type ftw! echo $diff->days; // $diff dateinterval object, echo it's 'days' property. // output: 3 (as of writing)
further reading:
http://php.net/manual/en/class.dateinterval.php
http://php.net/manual/en/class.datetime.php
http://php.net/manual/en/function.date-diff.php
Comments
Post a Comment