hi,
i have the following to calculate working days between 2 dates, but i am not getting the right amount. i should be getting 11 instead of 12, because feb 16 2009 was a holiday. can someone please advise? here is my code: -
<?php
-
//The function returns the no. of business days between two dates and it skips the holidays
-
function getWorkingDays($startDate,$endDate,$holidays){
-
//The total number of days between the two dates. We compute the no. of seconds and divide it to 60*60*24
-
//We add one to inlude both dates in the interval.
-
$days = (strtotime($endDate) - strtotime($startDate)) / 86400 + 1;
-
-
$no_full_weeks = floor($days / 7);
-
$no_remaining_days = fmod($days, 7);
-
-
//It will return 1 if it's Monday,.. ,7 for Sunday
-
$the_first_day_of_week = date("N",strtotime($startDate));
-
$the_last_day_of_week = date("N",strtotime($endDate));
-
-
//---->The two can be equal in leap years when february has 29 days, the equal sign is added here
-
//In the first case the whole interval is within a week, in the second case the interval falls in two weeks.
-
if ($the_first_day_of_week <= $the_last_day_of_week){
-
if ($the_first_day_of_week <= 6 && 6 <= $the_last_day_of_week) $no_remaining_days--;
-
if ($the_first_day_of_week <= 7 && 7 <= $the_last_day_of_week) $no_remaining_days--;
-
}
-
else{
-
if ($the_first_day_of_week <= 6) {
-
//In the case when the interval falls in two weeks, there will be a Sunday for sure
-
$no_remaining_days--;
-
}
-
}
-
-
//The no. of business days is: (number of weeks between the two dates) * (5 working days) + the remainder
-
//---->february in none leap years gave a remainder of 0 but still calculated weekends between first and last day, this is one way to fix it
-
$workingDays = $no_full_weeks * 5;
-
if ($no_remaining_days > 0 )
-
{
-
$workingDays += $no_remaining_days;
-
}
-
-
//We subtract the holidays
-
foreach($holidays as $holiday){
-
$time_stamp=strtotime($holiday);
-
//If the holiday doesn't fall in weekend
-
if (strtotime($startDate) <= $time_stamp && $time_stamp <= strtotime($endDate) && date("N",$time_stamp) != 6 && date("N",$time_stamp) != 7)
-
$workingDays--;
-
}
-
-
return $workingDays;
-
}
-
-
-
-
-
$holidays=array("2008-12-25","2008-12-26","2009-02-16");
-
-
echo getWorkingDays("2009-02-01","2009-02-17",$holidays)
-
-
-
-
?>
-
thanks in advance,
geebee
1 1556
In line 6 you have $days = ... + 1?? Why is that "+ 1" there?
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Johnson, Shaunn |
last post by:
Howdy:
Running PostgreSQL 7.2.1 on RedHat Linux 7.2.
How can I convert data in a table that has been created
with the INTERVAL data type into a numeric format?
Say, I have a table with this...
|
by: BlackFireNova |
last post by:
I need to write a report in which one part shows a count of how many
total records fall within the working days (Monday - Friday) inside of a
(prompted) given date range, in a particular...
|
by: james |
last post by:
I have a problem that at first glance seems not that hard to figure out. But, so far, the answer has escaped me. I have an old
database file that has the date(s) stored in it as number of days.
An...
|
by: Ricardo Perez Lopez |
last post by:
Hello everyone:
I'm a PostgreSQL newbie, working now with dates, times, timestamps and
intervals.
I have three questions about the above:
FIRST:
--------
|
by: Libber39 |
last post by:
Hi everyone,
Have a query on how to calculate the amount of weeks and days contained in a number in an access query. ie: the difference in days between 2 dates amounts to 17 days. I want to now...
|
by: hharry |
last post by:
Hello All,
I'm writing an app to track file transfer activity.
I have this enum to represent days of the week:
Monday = 1
Tuesday = 2
Wednesday = 3
Thursday = 4
|
by: =?Utf-8?B?QWw=?= |
last post by:
I am working in vb2005. how can I calculate business days (not including
holidays and weekends) between 2 dates? thanks
Al
|
by: Mike |
last post by:
I have a routine that's calculating business days but its not counting the
weekend days that are between the start date and end date. If my start date
is 9/26/08 and my end date is 10/01/08, I...
|
by: FishVal |
last post by:
IMHO, the following is not a how-to-do instruction to solve a particular problem but more a concept-proof stuff demonstrating possibilities of SQL.
So, let us say the problem is to calculate...
|
by: geraldjr30 |
last post by:
hi,
i have the following:
<?php
echo"test";
//The function returns the no. of business days between two dates and it skips the holidays
function...
|
by: Faith0G |
last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome former...
|
by: ryjfgjl |
last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
|
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$) {
}
...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
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
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
| |