减去时间PHP

I am having trouble with subtracting times in PHP.

My times are:

$row['exp'] = 2017-03-31

and date('Y-m-d') = 2017-03-10 (today)

$datetime1 = strtotime($row['exp']);
$datetime2 = strtotime(date('Y-m-d'));
$secs = $datetime1 - $datetime2;// == <seconds between the two times>
$days = $secs / 86400;
echo $days;

My result is:

36520.958333333333

Use the PHP DateTime class instead, it has built in functions, such as DateTime::diff, which seems to be what you're looking for. The DateTime objects are much easier to work with too.

$today = new DateTime('today');
$date = new DateTime("2017-03-31");

echo $today->diff($date)->d; // 21

Just pass your $row['exp'] as the parameter of $date instead, so you get

$date = new DateTime($row['exp']);

Although, I can't reproduce your issue with the given input you have, this live demo outputs "20.958333333333" (which is about right, you just need to ceil() it).

Live demo

References