Jump to content
Terminaxx

How to show the Timedifference?

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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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');
Edited by Jacques1

Share this post


Link to post
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 this post


Link to post
Share on other sites

Join the conversation

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

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

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


×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.