2

I got the time difference between dates like this :

$time1 = "2013-02-25 12:00:00";
$time2 = "2013-01-01 12:00:00";
$tdiff = strtotime($time1) - strtotime($time2);

I want extract days, hours and minutes from $tdiff. Output should like : 35 days 6 hours 14 minutes

I really searched and try to do something by myself. But I can't get true value.

---- EDIT ---- I can found date diff. I want extract days, hours, minutes from calculated time...

---- EDIT 2 ---- Here is my complete mysql code

select (
    select avg(UNIX_TIMESTAMP(tarih)) from table1 
    where action in (6) and who = '".$user."' and dates between '".$date1."' and '".$date."'
    ) - ( 
    select avg(UNIX_TIMESTAMP(tarih_saat)) from table2 
    where action in (6) and active = 1 and dates between '".$date1."' and '".$date2."
)

This query returns to me true value of time. This query is working correctly for me. Result is like : 215922. result type of UNIX_TIMESTAPM. So I want to learn how many days, hours and minutes in this timestamp.

Kara
  • 6,115
  • 16
  • 50
  • 57
Yasin Yörük
  • 1,500
  • 7
  • 27
  • 51
  • possible duplicate of [How to calculate the difference between two dates using PHP?](http://stackoverflow.com/questions/676824/how-to-calculate-the-difference-between-two-dates-using-php) – Rikesh Feb 25 '13 at 09:20
  • 1
    http://www.php.net/manual/en/datetime.diff.php – Buksy Feb 25 '13 at 09:21
  • Are those dates coming from a database originally? – Cups Feb 25 '13 at 09:21
  • @Cups yes. they coming from DB – Yasin Yörük Feb 25 '13 at 09:26
  • This answer http://stackoverflow.com/questions/7836384/get-remaining-days-hours-and-minutes-using-mysql explains how you could do this in your sql statement (mysql) and links to the manual page on time/date functions without evoking PHP classes. – Cups Feb 25 '13 at 10:20

2 Answers2

10

PHP has a DateInterval class which can be used like so:

$date1 = new DateTime('2013-02-25 12:00:00');
$date2 = new DateTime('2013-01-01 12:00:00');

$diff = $date2->diff($date1); // Get DateInterval Object

echo $diff->format('%d Day and %h hours and %i minutes');
Husman
  • 6,819
  • 9
  • 29
  • 47
  • 1
    `%m` in `format()` is incorrect! The minute should use `%i`. Here is reference ( https://www.php.net/manual/en/dateinterval.format.php ). – vee Apr 04 '19 at 14:14
-1

you could use date object to get more accurate.
the default var type doesnt give the time difference.
most often they are considered as string types when assinged like you did it

Pannan Mp
  • 77
  • 10