获取日期返回零之间的差异

I have a date time in 'Y-m-d H:i:s', and i tried to substract the now date with the defined date +1 day to get remaining time in hours, minutes and seconds:

$time = '2017-10-05 14:54:03';
$now =  date('Y-m-d H:i:s');
$endTransaction = date('Y-m-d H:i:s', strtotime($time. ' + 1 day'));
$dteDiff  = $endTransaction - $now;
echo $dteDiff;

but i always get 0 as the result

You are doing it wrong. The date function returns string so PHP is not able to compare anything. Try with the DateTime class instead. Its diff method returns the DateInterval object with some public properties, like the days property among others, which is the positive integer number (rounded down) of days between two dates:

$now = new \DateTime();
$endTransaction = (new \DateTime('2017-12-05 14:54:03'))->modify('+1 day');

$diff = $endTransaction->diff($now);

printf(
    'Difference in days: %d, hours: %d, minutes: %d, seconds: %d',
     $diff->days,
     $diff->h,
     $diff->m,
     $diff->s
);

You probably need to use this date_diff

    $time = '2017-10-05 14:54:03';
    $now =  date_create(date('Y-m-d H:i:s'));
    $endTransaction = date_create(date('Y-m-d H:i:s', strtotime($time. ' + 1 day')));
    $dteDiff  = date_diff($now, $endTransaction);
    $date = new DateTime($dteDiff);

    $result = $date->format('Y-m-d H:i:s');

According to above mentioned description please try executing following code snippet as a solution to it.

    $time = '2017-10-05 14:54:03';
    $now =  strtotime(date('Y-m-d H:i:s'));
    $endTransaction = strtotime(date('Y-m-d H:i:s', strtotime($time. ' + 1 day')));
    $dteDiff  = ($endTransaction - $now)/(24*60*60);
    echo round($dteDiff);

$endTransaction and $now are strings.

$time = '2017-10-05 14:54:03';
$now =  date('Y-m-d H:i:s');
$endTransaction = date('Y-m-d H:i:s', strtotime($time. ' + 1 day'));
echo($endTransaction."
");
echo($now."
");

It prints:

2017-10-06 14:54:03
2017-10-05 11:45:39

The subtraction is not a valid operation for strings. It can handle only numbers. The strings above are converted to numbers. The conversion uses only the leftmost digits present in the string, until it reaches the first character that is not a digit.

Both strings above produce 2017 when they are converted to numbers and their difference is, of course, 0.


The easiest way to work with dates in PHP is to use the DateTime and its related classes.

// Convert the input string to a DateTime object
$then = new DateTime('2017-10-05 14:54:03');
// Add 1 day
$then->add(new DateInterval('P1D'));
// Get the current date and time
$now  = new DateTime('now');
// Compute the difference; it is a DateInterval object
$diff = $now->diff($then);

// Display the dates and the difference
echo('Then: '.$then->format("Y-m-d H:i:s
"));
echo('Now : '.$now->format("Y-m-d H:i:s
"));
echo('Remaining: '.$diff->format("%R%a days, %h hours, %i minutes, %s seconds.
"));

The output:

Then: 2017-10-06 14:54:03
Now : 2017-10-05 12:36:25
Remaining: +1 days, 2 hours, 17 minutes, 38 seconds.