0

I want to calculate the difference between two dates

$startDate = date_create(date('Y-m-d H:i:s'));
$targetDate = date_create('2018-01-30 11:00:00');

$result = strtotime($targetDate) - strtotime($startDate);
$result = (int) $result;

echo $result;

but returned

Warning: strtotime() expects parameter 1 to be string, object given in /home/info.php on line 50

how can I solve this problem?

Eddie
  • 26,593
  • 6
  • 36
  • 58
ryankim
  • 1
  • 1

1 Answers1

0

Remove the date_create function altogether. date('Y-m-d H:i:s'); is returning a string which is the input that strtotime expects. Tested using php 5.2.17 at the sandbox link below.

$startDate = date('Y-m-d H:i:s');

$targetDate = '2018-01-30 11:00:00';

$result = strtotime($targetDate) - strtotime($startDate);

$result = (int) $result;

echo $result;

http://sandbox.onlinephpfunctions.com/code/9c59a5cb098eb80b355b947e394b3b0446852df3

JasonB
  • 6,243
  • 2
  • 17
  • 27