Jump to content

find what months a date range covers


searls03

Recommended Posts

I am looking for a way to grab a start date and an end date from my database, and find out what months of what years the range covers.  So for example, I have a march 15 2013 startdate, and a june 4 2013 enddate.  I would like something that could tell me that this range covers march 2013, april 2013, may 2013, and july 2013.  I know I could use a timestamp to find if a date falls within a specified month, but I can't figure out how I would reverse that into a months covered by range.  any help is appreciated.

Edited by searls03
Link to comment
Share on other sites

Here is how you might do it

 

1) Start date. Get year and month. EX: 2012 / 01

 

2) End Date. Get year and month. EX: 2013 / 02

 

Psuedo code

date = start date

while (date <= endDate) {
    monthsCovered[] = date('month');
    date = date + 1 month
}
Edited by teynon
Link to comment
Share on other sites


$months = array(1=>'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sept', 'Oct', 'Nov', 'Dec');
$end_m = date('n', $end);
$start_m = date('n', $start);
if($end_m >= $start_m){
$month_range = array_slice($months, $start_m, $end_m);
}else{
$month_range = array_merge(array_slice($months, $start_m), array_slice($months, 0, $end_m);
}
Not tested, and only supports 11 months or less range.
Link to comment
Share on other sites

Decided to have a little fun with this one.

<?php
    if (!interface_exists("Object_Array")) {
        // Blend the interfaces.
        interface Object_Array extends Iterator, Countable {
        }
    }
    class dateRange implements Object_Array {
        private $stopDate, $startDate, $pointer, $mode;
        public $output;
        
        public function __construct($start, $stop, $mode = "month", $output = "F Y") {
            $this->startDate = strtotime($start);
            $this->stopDate = strtotime($stop);
            $this->pointer = $this->startDate;
            $this->mode = $mode;
            $this->output = $output;
        }
        
        public function current() {
            return date($this->output, $this->pointer);
        }
        
        public function key() {
            return $this->pointer;
        }
        
        public function next() {
            switch ($this->mode) {
                default: // Get next month
                    $this->pointer = mktime(0,0,0, date("m", $this->pointer) + 1, 1, date("Y", $this->pointer));
                    break;
            }
        }
        
        public function rewind() {
            $this->pointer = $this->startDate;
        }
        
        public function valid() {
            if ($this->pointer <= $this->stopDate) {
                return true;
            }
            return false;
        }
        
        public function count() {
            $total = 0;
            switch ($this->mode) {
                default:
                    $start = $this->startDate;
                    while ($start < $this->stopDate) {
                        $start = mktime(0, 0, 0, date("m", $start) + 1, 1, date("Y", $start));
                        ++$total;
                    }
                    break;
            }
            
            return $total;
        }
    }
    
    $date = new dateRange("1/31/2010", "5/31/2012");
    
    foreach ($date as $monthYear) {
        echo $monthYear . "<br />";
    }
    
    echo count($date);

Edited by teynon
Link to comment
Share on other sites

try

$start = new DateTime('2013-01-31');
$end = new DateTime('2013-06-01');

$inc = DateInterval::createFromDateString('first day of next month');
$end->modify('+1 day');

$p = new DatePeriod($start,$inc,$end);

foreach ($p as $d)
 echo $d->format('M') . '<br>';

 

RESULTS:

 

Jan
Feb
Mar
Apr
May
Jun

Link to comment
Share on other sites

I thought I would be able to figure this stuff out, but here is what my ultimate goal is.  I am building a member registration system and I found this code from someone else on here that does exactly what I had been looking for for a pagination of months.

 

$offset = isset($_GET['offset']) ? intval($_GET['offset']) : 0;
$currMonth = date('n');

$list = array();
$list[] = mktime(0, 0, 0, $currMonth+$offset, 1);
$list[] = mktime(0, 0, 0, $currMonth+$offset+1, 1);
$list[] = mktime(0, 0, 0, $currMonth+$offset+2, 1);

foreach ($list as $dateTS)
{
   echo date('M Y', $dateTS) . '<br />';
}

$prev = $offset-3;
$next = $offset+3;
echo "<br><a href='?offset={$prev}'>Prev</a> <a href='?offset={$next}'>Next</a>"

 

 Any ways, what this does is creates a list of three months with pagination for moving onto next three months or previous three months.  I then am trying to build this piece of code from this topic, where it will find all events that fall underneath of the proper month(s).  So I have an event called test1 with the dates of 3/15/2013 to 7/18/2013.  What I then want it to do is display in a list like this

 

Page 1

March 2013(heading from code I provided)

          3/15/2013 - 7/18/2013 Test1

April 2013

          3/15/2013 - 7/18/2013 Test1

May 2013

          3/15/2013 - 7/18/2013 Test1

 

Page 2

June 2013

          3/15/2013 - 7/18/2013 Test1

July 2013

          3/15/2013 - 7/18/2013 Test1

August

       Left blank

 

my table structure has start and end date and event name and id.  

Edited by searls03
Link to comment
Share on other sites

ok, I finally got some error messages, this is what I received:

 

Warning: Unknown: It is not safe to rely on the system's timezone settings. You are *required* to use the date.timezone setting or the date_default_timezone_set() function. In case you used any of those methods and you are still getting this warning, you most likely misspelled the timezone identifier. We selected 'America/Los_Angeles' for 'PST/-8.0/no DST' instead in Unknown on line 0

Fatal error: Uncaught exception 'Exception' with message 'DateTime::__construct() [<a href='datetime.--construct'>datetime.--construct</a>]: It is not safe to rely on the system's timezone settings. You are *required* to use the date.timezone setting or the date_default_timezone_set() function. In case you used any of those methods and you are still getting this warning, you most likely misspelled the timezone identifier. We selected 'America/Los_Angeles' for 'PST/-8.0/no DST' instead' in /ssl/index.php:13 Stack trace: #0 /ssl/index.php(13): DateTime->__construct('2013-01-31') #1 {main} thrown in /ssl/index.php on line 13

Link to comment
Share on other sites

got it!!!!

 

 

date_default_timezone_set('America/Los_Angeles');
 
$start = new DateTime('2013-01-31');
$end = new DateTime('2013-06-01');
 
$inc = DateInterval::createFromDateString('first day of next month');
$end->modify('+1 day');
 
$p = new DatePeriod($start,$inc,$end);
 
foreach ($p as $d)
  echo $d->format('M') . '<br>';
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.