Subesh Pokhrel

Magento Developers Blog

Calculating the Difference Between Timestamps in PHP

Most of us, programmer have conditions of calculating the difference between timestamps, i.e the difference between now and the timestamp from mysql result. The logic for this is pretty simple. Just convert the two timestamps to Unix time format and take the difference between the two. Next convert the difference into corresponding days,hour,minutes or in any unit you want to. Here’s the source code for finding out the difference between two timestamps. [sourcecode lang=”PHP”] [/sourcecode] [sourcecode lang=”PHP”] [/sourcecode] There are two functions here expiredTime ( ) and countdown ( ). The input for the expiredTime is the timestamp from the mysql result. This function evaluates the value of year,month,hour,minutes and seconds of the input parameter and then calls the next function countdown ( ), with those evaluated values as inputs. The countdown function then calculates the time now and the difference code block 2 #08. Then the difference is divided accordingly, with appropriate denominators to find out the days,hour,minutes,seconds passed the input time, untill now. Code block 2 #10-13 is simple mathematics. This function then returns the difference value in days,hour,minutes and seconds.