2

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 :)

Rohit Suthar
  • 3,528
  • 1
  • 42
  • 48
Marverick M.
  • 124
  • 1
  • 15
  • Possible duplicate of [Subtract some date and time from current date and time to find age in PHP](http://stackoverflow.com/questions/38718155/subtract-some-date-and-time-from-current-date-and-time-to-find-age-in-php) – Kevin Kopf Nov 05 '16 at 10:13
  • I tried that but it doesn't work for me. It keeps loading. – Marverick M. Nov 05 '16 at 10:16

4 Answers4

5

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
Kevin Kopf
  • 13,327
  • 14
  • 49
  • 66
  • this one worked what is the requirement version of the php to use this? – Marverick M. Nov 05 '16 at 10:25
  • `5.2.0`. The question I posted as duplicate in the comment doesn't provide a straight answer, it provides a hint and if you wish to be a good programmer, you have to follow, analyze and research hints. Not everyone and not every time will provide a straight-forward answer. – Kevin Kopf Nov 05 '16 at 10:28
3
<?php
$db_user_timestamp = 1496592689;
$difference = $db_user_timestamp - time();
echo "Day : ".$day = date('d',$difference);
echo "<br>Hour : ".$hour = date('H',$difference);
echo "<br>Minute : ".$minute = date('i',$difference);
?>
Vishal Bareja
  • 128
  • 2
  • 11
2

If your PHP version is 5.3 or latest, you should check http://php.net/manual/en/class.dateinterval.php and http://php.net/manual/en/datetime.diff.php

$datetime1 = new DateTime(date('Y-m-d H:i:s', $db_user_timestamp));
$datetime2 = new DateTime(date('Y-m-d H:i:s'));
$interval = $datetime1->diff($datetime2);
echo $interval->format('%y years %m months %d days %h hours %m minutes %s seconds');
Nikolas
  • 636
  • 4
  • 10
0

Using date('M/d/Y H:i:s', $theTimestamp); will give you date in day, month, year, hour, minute, seconds in the order you want (change 'M/d/Y H:i:s' to your string depending on http://php.net/manual/en/function.date.php)