0

Im Having A Slight Issue Of The Output Of This Below

The Expiry Date In My Database Is 2017-12-02 06:32:46

But The Code Below Outputs It As 1969 Years, 12 Months, 18 Days

$target = strtotime("Y-m-d H:i:s", $user->getFromTable_MyId("expiry_date", "users")); //date in db = 2017-12-02 06:32:46
$today =  date("now");
$difference = $target - $today;
$year = date('Y',$difference);
$month = date('m',$difference);
$days = date('d',$difference);
print $year." Years,  ".$month." Months,  ".$days." Days";
chris85
  • 23,846
  • 7
  • 34
  • 51
  • 2
    You're using [strtotime()](http://php.net/manual/en/function.strtotime.php) wrong – Patrick Q Dec 01 '17 at 18:02
  • 1
    You are using the wrong functions. `date("now")` should be `time()`. Your `strtotime` should be `date`. The subtraction should be on the unix timestamp, not on the time string. – chris85 Dec 01 '17 at 18:03
  • i used `date($user->getFromTable_MyId("expiry_date", "users"));` i also tried i used `date("Y-m-d H:i:s", $user->getFromTable_MyId("expiry_date", "users"));` – Kyle Fardy Dec 01 '17 at 18:05
  • Use the `edit`, https://stackoverflow.com/posts/47598991/edit, link to update the question. There are multiple issues here. If the "timestamp" is really a datetime you need to convert it to the string. – chris85 Dec 01 '17 at 18:07

1 Answers1

0

This line is incorrect;

$target = strtotime("Y-m-d H:i:s", $user->getFromTable_MyId("expiry_date", "users"));

it should be

$target = strtotime($user->getFromTable_MyId("expiry_date", "users"));

EDIT, and date("now") should be time()

AND see this answer here https://stackoverflow.com/a/10696762/3790921

The rest of your "calculating" code is also not going to work. It is merely giving you a date like 1970-01-01 12:13:03

Your var $difference is the seconds between the 2 dates. You must calculate the hours, minutes, and seconds using normal math.

Chad
  • 1,139
  • 17
  • 41