Solved

Calculate Number of Days, Excluding Sundays, Between Two Dates

Posted on 2008-06-19
1
2,596 Views
Last Modified: 2013-12-13
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;

Open in new window

0
Comment
Question by:FrankTech
1 Comment
 

Accepted Solution

by:
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;

?>

Open in new window

0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
mimic google as my ip 11 53
php ssh2_scp_send 1 46
php string masking 4 27
html input type 3 21
This article will explain how to display the first page of your Microsoft Word documents (e.g. .doc, .docx, etc...) as images in a web page programatically. I have scoured the web on a way to do this unsuccessfully. The goal is to produce something …
Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
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…
The viewer will learn how to count occurrences of each item in an array.

919 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now