Marverick M. Marverick M. - 1 month ago 11
PHP Question

days, hours, and miniutes remaining in php

I have this timestamp from the database

1496592689


the problem is I don't know how to get the remaining days, hours and minutes

but I have this code bellow

this is my variable from where the timestamp stored
$db_user_timestamp


and now I have this current time now
$timenow = time();


I tried to calculate it with

$remainingtime = $db_user_timestamp - $timenow;


But I don't know how to put it in the days, hours and minutes.

Thanks in advance for helping me :)

Answer

Always use DateTime

$create_time = "1496592689";
$current_time = time();

$dtCurrent = DateTime::createFromFormat('U', $current_time);
$dtCreate = DateTime::createFromFormat('U', $create_time);
$diff = $dtCurrent->diff($dtCreate);

$interval = $diff->format("%y years %m months %d days %h hours %i minutes %s seconds");
$interval = preg_replace('/(^0| 0) (years|months|days|hours|minutes|seconds)/', '', $interval);

echo $interval;

result

 6 months 30 days 5 hours 52 minutes
Comments