need help with timestamp

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View

I have to get the difference between date 1 and date 2 (that is date 2 minus
date 1) ; the format is timestamp.

How can I do it? Does anybody have an example of code?

Thank you..


Re: need help with timestamp

intoned upon the aether:

Quoted text here. Click to load it

Since a PHP timestamp,

$time = time();

measures seconds since the Unix epoch (19700101) you simply want the
absolute value of the difference between the two values to get the
amount of time in seconds.

$diff = abs($date1-$date2);

$days = round($diff/86400, 0);
$hours = round(($diff%86400)/3600, 0);
$minutes = round(($diff%3600)/60, 0);
$seconds = $diff%60;

60 seconds == 1 minute
3600 seconds == 1 hour
86400 seconds == 1 day

Note that % is the modulus operator which returns the remainder from
division.  Hence:

4%3 == 1
5%3 ==2
5%4 == 1
6%3 == 0
6%4 == 2
7%3 == 1
7%4 == 3

The behavior with negative numbers in PHP is odd here in that if the
first number is negative, then the return value will also be negative.
But by using the abs() function (absolute value) one can ignore this.
Additionally, the round() function is used here to get rid of the
fractional portion of the return value of the division.

Beyond that, there are many time and date functions in PHP that may or
may not be helpful.  Take a look at the manual.



"In the End, we will remember not the words of our enemies,
 but the silence of our friends."

- Martin Luther King Jr. (1929-1968)

Photo Archive @
Last Updated 29 Sept. 2004

Site Timeline