Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
Solved

# Calculate Number of Days, Excluding Sundays, Between Two Dates

Posted on 2008-06-19
Medium Priority
2,844 Views
I'm using this code to calculate the difference (number of days) between any two dates.
Both \$begin_mk and \$end_mk are in YYYY-mm-dd format, like   2008-06-19 .

How could I modify it to exclude Sundays from the calculation?

For example, if there are 90 calendar days from 2008-01-01 to 2008-03-31 , then I guess I need to:
1) calculate the number of Sundays between those two dates, and
2) subtract the number of Sundays from the total number of days (90, in this case)

How to calculate the number of Sundays between any two dates? Or is there some better, easier way to do this?
``````\$begin_mk = strtotime("\$begin");
\$end_mk = strtotime("\$end");

\$datediff = ( \$end_mk > \$begin_mk ? ( \$end_mk - \$begin_mk ) : ( \$begin_mk - \$end_mk ) );
\$days = ( ( \$datediff / 3600 ) / 24 );
\$days = \$days + 1; // to be inclusive of last date;
``````
0
Question by:FrankTech
[X]
###### Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points
1 Comment

Accepted Solution

FrankTech earned 0 total points
ID: 21824609
Well, I finally came up with this.  Anybody have a better or easier way?
``````<?php

// ** Set Beginning and Ending Dates, in YYYY-mm-dd format **
\$begin = '2008-01-01';
\$end = '2008-03-31';
\$begin_mk = strtotime("\$begin");
\$end_mk = strtotime("\$end");

// ** Calculate number of Calendar Days between the two dates
\$datediff = ( \$end_mk > \$begin_mk ? ( \$end_mk - \$begin_mk ) : ( \$begin_mk - \$end_mk ) );
\$days = ( ( \$datediff / 3600 ) / 24 );
\$days = \$days + 1; // to be inclusive of last date;

// ** Count days excluding Sundays **
\$iteration = 0;
\$numDaysExSunday = 0;
for (\$i=1; \$i<=\$days; \$i++) {
\$weekday = date("w", strtotime("\$begin + \$iteration day"));
echo "\$weekday<br>";
if (\$weekday !== '0') {
\$numDaysExSunday++;
}
\$iteration++;
}

// ** Output number of days excluding Sundays **
echo \$numDaysExSunday;
?>
``````
0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this. Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it i…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
###### Suggested Courses
Course of the Month8 days, 17 hours left to enroll