Jump to content

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
Link to comment
https://forums.phpfreaks.com/topic/304461-how-to-show-the-timedifference/
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

 

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. 

This thread is more than a year old. Please don't revive it unless you have something important to add.

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.