How to display days and hours with date() in php

Tags: , ,



I have a timestamp which calculates remaining time between 2 dates.

$job_expiration = strtotime($job['job_expiration']) - time(); like so.

Right now, I’m showing only 1 day or 2 days with the date() function.

date('j', $job_expiration) like so.

I also want to show hours instead of 2 days. let’s say the time remaining is 1 day 16 hours or only 30 minutes. then I want to show the hours with day too.

1 day, 16 hours or only 30 minutes

how can I do that?

Answer

function convert_seconds($seconds) {
  $dt1 = new DateTime("@0");
  $dt2 = new DateTime("@$seconds");
  return $dt1->diff($dt2)->format('%a days, %h hours, %i minutes and %s seconds');
}
echo convert_seconds(200000)."n";

Sample output:

2 days, 7 hours, 33 minutes and 20 seconds

In your case you should use this function like convert_seconds($job_expiration)



Source: stackoverflow