473,386 Members | 1,743 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

need help with timestamp

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
1 2016
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Ariant | last post by:
Hi - I'm trying to query my database to find the min, max and average times (in seconds, or minutes, or something) between two timestamps. I've tried using: Select avg(timestamp1 -...
2
by: Fabien Penso | last post by:
Hi. I need a little help to optimize requests on a table, and I have no idea how to do it (I optimized already a lot other table, but this one is a pain). My table looks like : CREATE TABLE...
0
by: Steve Thorpe | last post by:
Hi I am trying to write a report that calculates the average number of sales over 7, 14, 31 and 365 days for each hourly period of the day. the problem is it takes over 4 minutes to run. ...
1
by: Steve Thorpe | last post by:
Hi I am trying to write a report that calculates the average number of sales over 7, 14, 31 and 365 days for each hourly period of the day. the problem is it takes over 4 minutes to run. ...
2
by: Astra | last post by:
Hi All I know an SQL Server timestamp seems to be as useful as rocking horse for show jumping, but I'm hoping you know a 'fudge' to get me round a problem or at least confirm that it isn't...
1
by: Jill | last post by:
I am trying to store the current date to an ms access database on my server. I set it up with a dsnless connection. Here is the statement: Insert Into employees(timestamp) Values ('" & date() &...
2
by: rong.guo | last post by:
Hello Group, I am having a really weird problem... Can anyone tell the difference between Query 1 and Query 2 below? Why Query 2 excludes '2/28/2005'? Many thanks! create table a...
2
by: Russell Smith | last post by:
Timestamps support infinity. However if appears dates do not. When timestamps are cast to dates, there is no output. Is this an acceptable option or not? Below are a number of examples...
3
by: shiranaomi | last post by:
I'm wondernig if it's possible to do the following: I'd like to average every 2 numeric values in a table as they appear sequentially ordered by their time stamps. I was thinking the only way to...
9
by: Paulers | last post by:
Hello, I have a log file that contains many multi-line messages. What is the best approach to take for extracting data out of each message and populating object properties to be stored in an...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.