473,804 Members | 3,725 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Date difference in days....

Probably being a little thick here, but when you subtract one date away
from another, how do you convert the resultant value into a number of
days... I guess I could easily / 60 / 60 / 24... but that seems
barbaric... Anything neater?

Cheers
Simon

Ps, Im also just trying to work out how to calculate the number of
mondays and tuesdays etc between two dates... Just thought I'd run this
theory past you if I may to gain more confidence.

I figure getting the date difference, and knowing that the starting date
falls on a Thursday holds the key... working out the days between these
two dates should be easy enough to divide by 7 to get the whole number
and remainder (is that a mod?)... we can just say there's x number of
Wednesdays, x number of Thursdays etc, and then just loop through the
remainder which is a small number so not much of system load?

then we can work out how many of those days were working days, subtract
holiday dates from each count... and... well, that's my thoughts so far...

Workable?
Dec 14 '05 #1
5 3256
Simon Dean wrote:
... subtract one date away from another
... convert [...] into a number of days
... calculate the number of mondays and tuesdays etc between two dates <snip> Workable?

Check these out:
http://aidan.dotgeek.org/repos/?file=Duration.php
http://pear.php.net/package/Date/
http://pecl.php.net/package/date_time

I got the links from the php.net manual page for date().
Other than checking they're not dead links I haven't tested them.
Right now it's the second 'User Contributed Note' from "aidan at php
dot net".

Dec 14 '05 #2
>Probably being a little thick here, but when you subtract one date away
from another, how do you convert the resultant value into a number of
days... I guess I could easily / 60 / 60 / 24... but that seems
barbaric... Anything neater?
First off, you need to nail down what answer you want a little
more. You will not always get agreement on these:

1. How many days are there between January 1 and January 2?
My answer: NONE, those two days are next to each other, and
therefore none of them are Mondays regardless of what year it is.

2. How many days are there between January 2 and January 2 (of
the same year)? My answer: this doesn't really make sense, but
if you insist, negative one.

If, on the other hand, you're trying to figure out how many work
days there are between the morning of date X and the evening of date Y,
some people might consider the answers to the above questions as
2 and 1, respectively.
Ps, Im also just trying to work out how to calculate the number of
mondays and tuesdays etc between two dates... Just thought I'd run this
theory past you if I may to gain more confidence.

I figure getting the date difference, and knowing that the starting date
falls on a Thursday holds the key... working out the days between these
two dates should be easy enough to divide by 7 to get the whole number
and remainder (is that a mod?)... we can just say there's x number of
Wednesdays, x number of Thursdays etc, and then just loop through the
remainder which is a small number so not much of system load?


I had occasion to do some calculations to determine the next <day
of week> or first <day of week> in <month> after the given date.
Your calculations will likely involve a lot of % 7 or / 7 operations,
occasionally with adding 7 to avoid negative numbers being fed to
%. You can probably avoid a loop.

Gordon L. Burditt

Dec 15 '05 #3
Gordon Burditt wrote:
Probably being a little thick here, but when you subtract one date
away
from another, how do you convert the resultant value into a number
of

days... I guess I could easily / 60 / 60 / 24... but that seems
barbaric... Anything neater?

First off, you need to nail down what answer you want a little more.
You will not always get agreement on these:

1. How many days are there between January 1 and January 2? My
answer: NONE, those two days are next to each other, and therefore
none of them are Mondays regardless of what year it is.


Inclusive of the start day and end day.

2. How many days are there between January 2 and January 2 (of the
same year)? My answer: this doesn't really make sense, but if you
insist, negative one.
1.

If, on the other hand, you're trying to figure out how many work days
there are between the morning of date X and the evening of date Y,
some people might consider the answers to the above questions as 2
and 1, respectively.


I figure somehow, will need to somewhat ignore the time, maybe get the
two dates, set the time to the same value, then work out the day
difference and add one to get the result I want...

Still though, don't know how to convert the result of $date1 - $date2 to
something more useful, like, minutes, hours, or days.... That's the only
stumbling block as far as Im concerned, Im sure I can work out the other
bits.

Cheers
Simon
Dec 15 '05 #4
C.
The Pear package is not exactly well documented.

These all seem to rely on Unix timestamps - which are only cover a
relatively small interval (32 bits 1970 - 2038 IIRC).

Since most the stuff I write is talking to a DBMS anyway I usually use
the MySQL date manipulation functions. These include a datediff()
function.

HTH

C.

Dec 15 '05 #5
Simon Dean wrote (in part):
Probably being a little thick here, but when you subtract one date away
from another, how do you convert the resultant value into a number of
days... I guess I could easily / 60 / 60 / 24... but that seems
barbaric... Anything neater?


This the simpliest function I could come up with for getting the
difference between two dates. Note that I didn't code any error
checking.

<?php
echo date_diff ("20051210", "20051230") ;

function date_diff($s,$e )
{
return((strtoti me($e) - strtotime($s))/86400);
}
?>

Since the strtotime() function returns the number of seconds since
1/1/1970, you need to divide the result by the number of seconds in a
day (86400) to get the number of days.

Ken

Dec 15 '05 #6

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

Similar topics

7
11009
by: Ben | last post by:
Hi, What is the synatx in C# for calculating date difference between 2 dates in terms of no. of days? Thanks, Ben
4
15761
by: jamesyreid | last post by:
Hi, I'm really sorry to post this as I know it must have been asked countless times before, but I can't find an answer anywhere. Does anyone have a snippet of JavaScript code I could borrow which calculated the difference in years and days between two dates, and takes leap years into account? I'm calculating the difference in the usual way, i.e....
1
7271
by: rockysg | last post by:
I need to create a page which takes input from a field from MS Access Database which has one date and there is another field in the MS Access Database which has another date. I want to find the date difference in no of days from these two fields using ASP VBScript. Kindly Help me out.
2
3919
by: Drum2001 | last post by:
Hello, I am having isues with the following: I have two forms, a MAIN FORM with a SUB FORM: Within the MAIN FORM, I have an unbound textbox (Date Format) and a command button. Onload, the textbox defualts to Date(). The SUB FORM has a bound textbox (Date Format). I am trying to create the code for the command button to compare the
1
2741
by: Jim Slatter | last post by:
I am struggling with a simple task of finding the difference between the earliest date and latest date for records held in a sub-form. The dates are not necessarily always entered in the correct sequence, but the subform data re sorts itself after a new entry has been made so the first date is at the top and the last date at the bottom. Counting the records isn't a problem, but getting the difference betwen first and last date in days is. Can...
2
29769
by: kbaisch | last post by:
I have this code set up but I know the 3rd line is wrong. Could someone please help me on the code for pulling all entries for todays date -90 days? I would really appreciate it and thanks for helping. SELECT * from view1_4t where DOB_ = (getdate()-90) GO
7
2474
by: rdawadiuk | last post by:
hi, I would like to calculate the date difference between two dates using field names which I have in my table. The field name is " Account opened on" for eg If the "Account open on" is Jan 5, 2007 the I would like to calculate the date difference between this date and the end of the quarter (March 31 2007) I would like Acess to calculate this difference automatically without me entering the end of the quarter date. please help thanks
3
2111
by: Cyprus106 | last post by:
I can't seem to get a reliable function that grabs the actual date 30 days in the future. Adding +30 to the date() function doesn't seem to compensate for if the next month has less than the +30 calculated number of days and other little quirks like that that can generate a date that doesn't actually exist. Is there a more thorough method for getting a real date 30 days from today? Thanks a lot, everyone!
10
2242
by: sarah2855 | last post by:
I have a table called Customers, This table has CustomerName, Order, and Order Date: Customer1 Order A 1/1/2006 Order B 1/6/2006 Order C 1/1/2009 Customer2 Order A 1/1/2007 Order B 1/1/2010 Order C 1/6/2010
0
9579
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
10330
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
10319
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
10076
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
9144
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
6851
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
4297
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
2
3816
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2990
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.