How to show the Timedifference?

Go to solution Solved by Jacques1,

Recommended Posts

Hey Guys.

I got a timestamp saved in my database. Now i want to show the Timediffernce between now and the timestamp saved.

I tried this:

```                                    \$date1 = time();
\$date2 = \$enddate;
\$diffDate = \$date2 - \$date1;

\$days = floor(\$diffDate / 24 / 60 / 60 );
\$diffDate = \$diffDate - (\$days*24*60*60);
\$hours = floor(\$diffDate / 60 / 60);
\$diffDate = (\$diffDate - (\$hours*60*60));
\$minutes = floor(\$diffDate/60);
\$diffDate = \$diffDate - (\$minutes*60);
\$seconds = floor(\$diffDate);

echo "Time: ".\$days."d ".\$hours."h ".\$minutes."m ".\$seconds."s";
```

But it doesnt work. It shows something like this -19383d 16h 33m 37s

Thanks for any help

Edited by Terminaxx
Share on other sites

Could you show me an example on how to do it and also show the results in days, hours, minutes and seconds?

I would be very grateful

Edited by Terminaxx
Share on other sites

But the only thing i can do with it is see the DATEdifference between it. But i would also like to see the hour, minute and second diffrences.

Is this somehow possible?

Share on other sites

• Solution

There's no such feature in MySQL. The DATEDIFF() function gives you calendar days and has nothing to do with time.

Use the DateTime class in your application.

```<?php

const MYSQL_DATETIME_FORMAT = 'Y-m-d G:i:s';

\$input = '2017-07-31 19:15:74';

\$now = new DateTime();
\$input_datetime = DateTime::createFromFormat(MYSQL_DATETIME_FORMAT, \$input);

\$diff = \$now->diff(\$input_datetime);

echo \$diff->format('%d days, %H hours, %I minutes, %S seconds');
```
Edited by Jacques1
Share on other sites

There's no such feature in MySQL. The DATEDIFF() function gives you calendar days and has nothing to do with time.

Use the DateTime class in your application.

```<?php

const MYSQL_DATETIME_FORMAT = 'Y-m-d G:i:s';

\$input = '2017-07-31 19:15:74';

\$now = new DateTime();
\$input_datetime = DateTime::createFromFormat(MYSQL_DATETIME_FORMAT, \$input);

\$diff = \$now->diff(\$input_datetime);

echo \$diff->format('%d days, %H hours, %I minutes, %S seconds');
```

Thank you once again for a good answer with an example given.

Share on other sites

This thread is more than a year old.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

×   Pasted as rich text.   Restore formatting

Only 75 emoji are allowed.

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×

• Activity

• Chat
×
• Create New...