By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,921 Members | 1,187 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,921 IT Pros & Developers. It's quick & easy.

need help with timestamp

P: n/a
Hi!

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

Pero
Jul 17 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
On Sun, 30 Jan 2005 14:58:36 +0100, "roll" <ro**@net.hr> reverently
intoned upon the aether:
Hi!

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?


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.

http://us4.php.net/manual/en/function.time.php

enjoy,

Sean
"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 @ http://www.tearnet.com/Sean
Last Updated 29 Sept. 2004
Jul 17 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.