473,770 Members | 1,995 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Calculating the difference

Hej dudes,

I need to calc the difference between two timestamps / dates ...

For example what i need to calculate:

Date 1: 2007.11.06 - 20:13:04
Date 2: 2007.11.07 - 21:13:04

Difference: 1 day, 1hour

Very simple in php ..

but how do i calculate the difference between the following values:

Date 1: 2007.11.06 - 20:13:04
Date 2: 2007.11.08 - 03:13:04

Does someone have a quick solution for this ?
Thanks ahead ...

Oct 1 '07 #1
5 3488
Julius wrote:
Hej dudes,

I need to calc the difference between two timestamps / dates ...

For example what i need to calculate:

Date 1: 2007.11.06 - 20:13:04
Date 2: 2007.11.07 - 21:13:04

Difference: 1 day, 1hour

Very simple in php ..
Very simple?
>
but how do i calculate the difference between the following values:

Date 1: 2007.11.06 - 20:13:04
Date 2: 2007.11.08 - 03:13:04

Does someone have a quick solution for this ?
Thanks ahead ...
Why is the first simple and the second hard?

Anyway: go to www.php.net and look up: strtotime

Then use the difference in time to calculate the elapsed <whatever unit
suits you>.

Beware of timezone offset stuff. If it is not in the original date,
don't worry.

Regards,
Erwin Moller
Oct 1 '07 #2
On 1 Oct, 14:49, Julius <julius.ehrl... @googlemail.com wrote:
Hej dudes,

I need to calc the difference between two timestamps / dates ...

For example what i need to calculate:

Date 1: 2007.11.06 - 20:13:04
Date 2: 2007.11.07 - 21:13:04

Difference: 1 day, 1hour

Very simple in php ..

but how do i calculate the difference between the following values:

Date 1: 2007.11.06 - 20:13:04
Date 2: 2007.11.08 - 03:13:04

Does someone have a quick solution for this ?
Thanks ahead ...
First, http://pt.php.net/manual/en/function.strtotime.php. Then
$later_time - $earlier_time.

$date1 = '2005-12-25 00:56:27 GMT' ; // Note the timezone
specification
$time1 = strtotime($date 1) ;
$date2 = '2005-12-27 05:56:27 GMT' ; // Note the timezone
specification
$time2 = strtotime($date 2) ;

$difference = $time2 - $time1;

print date('z \d\a\y\s\, H:i:s', $difference - 3600); // X days,
Hours:Minutes:S econds

I added - 3600 because it was showing 01:00:00 even when $difference
was 0. Probably DST thingie.

Oct 1 '07 #3

"Julius" <ju************ @googlemail.com wrote in message
news:11******** **************@ n39g2000hsh.goo glegroups.com.. .
Hej dudes,

I need to calc the difference between two timestamps / dates ...

For example what i need to calculate:

Date 1: 2007.11.06 - 20:13:04
Date 2: 2007.11.07 - 21:13:04

Difference: 1 day, 1hour

Very simple in php ..

but how do i calculate the difference between the following values:

Date 1: 2007.11.06 - 20:13:04
Date 2: 2007.11.08 - 03:13:04
there is no 'difference'. you want to subtract those hours/minutes/seconds
from 2007-11-06...right?

$stamp = strtotime('2007-11-06');
$minus = explode(':', '20:13:04');
foreach ($minus as $increment =$value)
{
switch($increme nt)
{
case 0: $increment = ' hours'; break;
case 1: $increment = ' minutes'; break;
case 2: $increment = ' seconds'; break;
}
$stamp = strtotime('-' . $value . $increment, $stamp);
}
Oct 1 '07 #4

"Steve" <no****@example .comwrote in message
news:4k******** ***@newsfe06.lg a...
>
"Julius" <ju************ @googlemail.com wrote in message
news:11******** **************@ n39g2000hsh.goo glegroups.com.. .
>Hej dudes,

I need to calc the difference between two timestamps / dates ...

For example what i need to calculate:

Date 1: 2007.11.06 - 20:13:04
Date 2: 2007.11.07 - 21:13:04

Difference: 1 day, 1hour

Very simple in php ..

but how do i calculate the difference between the following values:

Date 1: 2007.11.06 - 20:13:04
Date 2: 2007.11.08 - 03:13:04
sorry, didn't take you oddball date notation as a single date stamp but as
the calculation. anyway, bruno's works fine.
Oct 1 '07 #5
On 1 Oct, 15:24, "Steve" <no....@example .comwrote:
"Steve" <no....@example .comwrote in message

news:4k******** ***@newsfe06.lg a...


"Julius" <julius.ehrl... @googlemail.com wrote in message
news:11******** **************@ n39g2000hsh.goo glegroups.com.. .
Hej dudes,
I need to calc the difference between two timestamps / dates ...
For example what i need to calculate:
Date 1: 2007.11.06 - 20:13:04
Date 2: 2007.11.07 - 21:13:04
Difference: 1 day, 1hour
Very simple in php ..
but how do i calculate the difference between the following values:
Date 1: 2007.11.06 - 20:13:04
Date 2: 2007.11.08 - 03:13:04

sorry, didn't take you oddball date notation as a single date stamp but as
the calculation. anyway, bruno's works fine.
It might work fine but I found out it only goes to 364 days of
distance, so here's a new code, completely fixed for that bug:

[PHP]
<?php
$date1 = '2005-12-25 00:56:27 GMT' ; // Note the timezone
specification
$date2 = '2005-12-27 05:56:27 GMT' ; // Note the timezone
specification
$time_fix = 3600;

$time1 = strtotime($date 1);
$time2 = strtotime($date 2) ;
$difference = $time2 - $time1;
$years = date('Y', $difference) - 1970;

print "$years years, ";
print date('z \d\a\y\s\, H:i:s', $difference - $time_fix); // X days,
Hours:Minutes:S econds
?>
[/PHP]

I removed the 1970 because that was the default value, so if the
distance is 0 years, then it will show up as 1970 - 1970 which is 0.

Also, in $time_fix, you can edit the number of seconds to remove from
the difference, so if you don't need to remove 3600 or whatever, you
can edit that easily.

Oct 1 '07 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
4492
by: Hans Gruber | last post by:
Hi all, I have been struggling with a problem all day, I have been unable to come up with a working solution. I want to write a function which takes 2 unix timestamps and calculates the difference. I want it to return the difference in years, months, days, hours, minutes and seconds (a complete summary). Keeping into account of course that these are 2 real dates, I dont want it to work with 30.475 as an average number of days in a...
12
12267
by: Ola Natvig | last post by:
Hi all Does anyone know of a fast way to calculate checksums for a large file. I need a way to generate ETag keys for a webserver, the ETag of large files are not realy nececary, but it would be nice if I could do it. I'm using the python hash function on the dynamic generated strings (like in page content) but on things like images I use the shutil's copyfileobject function and the hash of a fileobject's hash are it's handlers memmory...
10
6642
by: riki | last post by:
hello, i need to calculate num of days between 2 dates... i get separate parts of dates from html form, then i need to "make" begining and ending date and calculate difference between them... something like this, in sybase: DAYS(YMD(?YEAR?,?MONTH?,?DAY?), YMD(?YEAR1?,?MONTH1?,?DAY1?)) thnx
1
4309
by: Tony Williams | last post by:
I have a table with two fields, txtvalue (a number field) and txtmonth ( a date/time field). I want to create a report that shows the difference in value between the value in txtvalue in one value of txtmonth and the value of txtvalue in another value of txtmonth and the percentage increase . For example if I have the value 1000 in 30/03/03 and the value 1100 in 30/03/04 How do I calculate the difference as 100 and the increase as 10%. I...
3
12940
by: Ron Vecchi | last post by:
I need to calculate the age of a person based on a DateTime BirthDate I was thinking TimeSpan ts = DateTime.Now - BirthDate; //I can get the days but not years. // I could check each year from their year of birth, count the days in the year and compare with the days returned from timespan
3
1750
by: laurentc | last post by:
Dear all, I have a simple table, with only the following fields: - Key - MyDate - Price I would like to biuld a Query which uses these fields and which make some
2
11776
by: slinky | last post by:
Anyone know how to calculate the difference between two times displayed in two textboxes? I'm starting out with two textboxes: "txtCallTimeBegins" & "txtCallTimeEnds" this yielded: 6/7/2007 2:40:50 PM & 6/7/2007 2:45:04 PM respectively. First I have coverted these to two additional textboxes to hold only the time, not the date using: =TimeValue() & =TimeValue()
6
9022
by: krishnakant Mane | last post by:
hello, I am strangely confused with a date calculation problem. the point is that I want to calculate difference in two dates in days. there are two aspects to this problem. firstly, I can't get a way to convert a string like "1/2/2005" in a genuan date object which is needed for calculation. now once this is done I will create a another date object with today = datetime.datetime.now() and then see the difference between this today and...
8
1913
by: brasilino | last post by:
Hi Folks: I'm trying to calculating a substring length directly from pointer address, like this: char *e = NULL, *s = NULL; int len = 0; s = strchr (url,'.'); e = strrchr (url,'?');
0
9618
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9454
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10260
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10101
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10038
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9906
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8933
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5482
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4007
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.