Jump to content

How to show the Timedifference?


Terminaxx

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

Link to comment
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');
Link to comment
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. 

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.