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

x
?
Solved

determine all the dates between a start and end date.

Posted on 2003-10-25
4
Medium Priority
?
182 Views
Last Modified: 2013-12-12
I am trying to build a form (using PHP) and would like to have a start date (entered as 3 fields - startMonth, startDay, and startYear) and an end date (entered as 3 fields - endMonth, endDay, endYear).
If they pick the same day for both I am ok, but if the end date is  3 days later I have to figure what dates are in between.

Example 12/20/2003 - 12/23/2003, would have 12/20/2003 and 12/21/2003, and 12/22/2003, and 12/23/2003. (4 dates)

I am pretty new to programming, so an example would be helpful.

Thanks
0
Comment
Question by:James Stone
[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
  • Learn & ask questions
  • 2
4 Comments
 
LVL 13

Accepted Solution

by:
lozloz earned 500 total points
ID: 9622234
<?
if(!checkdate($_POST["startMonth"],$_POST["startDay"],$_POST["startYear"])) {
  die("Start date invalid");
}
if(!checkdate($_POST["endMonth"],$_POST["endDay"],$_POST["endYear"])) {
  die("end date invalid");
}
$startdate = mktime(0,0,0,$_POST["startMonth"],$_POST["startDay"],$_POST["startYear"]);
$enddate = mktime(0,0,0,$_POST["endMonth"],$_POST["endDay"],$_POST["endYear"]);
if($enddate < $startdate) {
  die("End date precedes start date");
}
$distance = $enddate - $startdate;
$distance = $distance / 60 / 60 / 24;
$distance = floor($distance);
?>

$distance should result in the number of days between them, tell me how you get on
0
 

Author Comment

by:James Stone
ID: 9622414
What I was missing was looping it through a mktime statement  x number of  times, finally came to me!

first time I had seen floor function so read up on that.

Thanks for the comment - eventually I will figure this stuff out.

<?

$distance = $distance / 60 / 60 / 24;
$distance = floor($distance);
echo $distance."<p>";

//
// loop through the days
$range = range (0, $distance);

foreach ($range as $value) {
        $Rangetimestamps = mktime(0,0,0,$_POST["Startmonth"],($_POST["Startday"]+$value),$_POST["Startyear"]);
        $RangeDates = date("mdy", $Rangetimestamps);
        //echo $Rangetimestamps. " and ".$value."<p>";
        echo $RangeDates."<p>";

}
?>
0
 
LVL 33

Expert Comment

by:snoyes_jw
ID: 9661074
going with lozloz's initial range checking and assignments using mktime, here's another way to loop through the dates which I think might be a little more readable when you come back to do some debugging:

while ($startdate <= $enddate)
{
    echo date("mdy", $startdate);
    $startdate = strtotime("+1 day", $startdate);
}
0
 

Author Comment

by:James Stone
ID: 9662901
Thanks, I am teaching my self php (I've worked with databases for a few years). The less typing the better! It is much easier to read and probably more efficient too.

Just realized I never gave lozloz his points for helping.

james
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
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 look for a specific file type in a local or remote server directory using PHP.

688 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