Elapsed time from a given time in the database

后端 未结 3 1472
悲哀的现实
悲哀的现实 2021-01-28 14:20

I have a HTML table which has records pulled in from the database. I\'m using PHP/MySQL.

The Column in my table named \"Timer\" is not retrieved from the database. I nee

3条回答
  •  被撕碎了的回忆
    2021-01-28 14:30

    This can be achieved with very little Javascript.

    enter image description here

    Assuming that the "Created" time is rendered dynamically in the table with format dd MMM yyyy hh:mm:ss, something like this should do the trick:

    
    
        
        
    
    
        
    CreatedTimer
    21 Feb 2013 12:30:00

    Copy the code above into a file, say index.html, and open it in a browser. I tested it on Chrome.

    It should update the elapsed time every 2 seconds, but you may change the update interval to something that suits you, e.g. to make it update every 5 minutes:

    new ElapsedTimeLogger("#date", "#elapsed", 300);

    The general concept is to parse the rendered "Created" date into an epoch timestamp (in milliseconds) and then compute its difference with the current system time. To get the elapsed time updating dynamically you use Javascript's setInterval function. To stop updating the elapsed time use Javascript's clearTimeout function.

    I lifted the prettyPrintTime function from powtac.

提交回复
热议问题