searls03 Posted March 6, 2013 Share Posted March 6, 2013 (edited) 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 March 6, 2013 by searls03 Quote Link to comment https://forums.phpfreaks.com/topic/275341-find-what-months-a-date-range-covers/ Share on other sites More sharing options...
teynon Posted March 6, 2013 Share Posted March 6, 2013 (edited) 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 March 6, 2013 by teynon Quote Link to comment https://forums.phpfreaks.com/topic/275341-find-what-months-a-date-range-covers/#findComment-1417051 Share on other sites More sharing options...
Barand Posted March 7, 2013 Share Posted March 7, 2013 Be careful when adding "1 month" Jan 31 + 1 month = Mar 3 Quote Link to comment https://forums.phpfreaks.com/topic/275341-find-what-months-a-date-range-covers/#findComment-1417061 Share on other sites More sharing options...
searls03 Posted March 7, 2013 Author Share Posted March 7, 2013 thats a good point, what is the best way to fix this? Quote Link to comment https://forums.phpfreaks.com/topic/275341-find-what-months-a-date-range-covers/#findComment-1417062 Share on other sites More sharing options...
Jessica Posted March 7, 2013 Share Posted March 7, 2013 $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. Quote Link to comment https://forums.phpfreaks.com/topic/275341-find-what-months-a-date-range-covers/#findComment-1417063 Share on other sites More sharing options...
teynon Posted March 7, 2013 Share Posted March 7, 2013 You start with the year and month as the start date. No day involved. Or default to 1 for day. Quote Link to comment https://forums.phpfreaks.com/topic/275341-find-what-months-a-date-range-covers/#findComment-1417064 Share on other sites More sharing options...
teynon Posted March 7, 2013 Share Posted March 7, 2013 (edited) 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 March 7, 2013 by teynon Quote Link to comment https://forums.phpfreaks.com/topic/275341-find-what-months-a-date-range-covers/#findComment-1417067 Share on other sites More sharing options...
Barand Posted March 7, 2013 Share Posted March 7, 2013 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: JanFebMarAprMayJun Quote Link to comment https://forums.phpfreaks.com/topic/275341-find-what-months-a-date-range-covers/#findComment-1417068 Share on other sites More sharing options...
teynon Posted March 7, 2013 Share Posted March 7, 2013 Use what Barand posted, way more functional than mine. Quote Link to comment https://forums.phpfreaks.com/topic/275341-find-what-months-a-date-range-covers/#findComment-1417069 Share on other sites More sharing options...
Jessica Posted March 7, 2013 Share Posted March 7, 2013 I really need to learn to use the new DateInterval stuff. Very cool. Quote Link to comment https://forums.phpfreaks.com/topic/275341-find-what-months-a-date-range-covers/#findComment-1417075 Share on other sites More sharing options...
searls03 Posted March 7, 2013 Author Share Posted March 7, 2013 barand, is there something I need to edit in the code you posted? not for sure. but I am receiving no results Quote Link to comment https://forums.phpfreaks.com/topic/275341-find-what-months-a-date-range-covers/#findComment-1417084 Share on other sites More sharing options...
searls03 Posted March 7, 2013 Author Share Posted March 7, 2013 (edited) 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 March 7, 2013 by searls03 Quote Link to comment https://forums.phpfreaks.com/topic/275341-find-what-months-a-date-range-covers/#findComment-1417085 Share on other sites More sharing options...
searls03 Posted March 7, 2013 Author Share Posted March 7, 2013 there may be an easier way to do this than what I am trying to do by combining a bunch of codes into one Quote Link to comment https://forums.phpfreaks.com/topic/275341-find-what-months-a-date-range-covers/#findComment-1417087 Share on other sites More sharing options...
teynon Posted March 7, 2013 Share Posted March 7, 2013 barand, is there something I need to edit in the code you posted? not for sure. but I am receiving no results What version of PHP are you using? Quote Link to comment https://forums.phpfreaks.com/topic/275341-find-what-months-a-date-range-covers/#findComment-1417090 Share on other sites More sharing options...
searls03 Posted March 7, 2013 Author Share Posted March 7, 2013 5.3.6 Quote Link to comment https://forums.phpfreaks.com/topic/275341-find-what-months-a-date-range-covers/#findComment-1417092 Share on other sites More sharing options...
teynon Posted March 7, 2013 Share Posted March 7, 2013 If I cut and paste exactly what Barand posts, it works fine for me. PHP.net says all those classes are available in 5.3. Are you getting an error? Do you know how to turn errors on? Quote Link to comment https://forums.phpfreaks.com/topic/275341-find-what-months-a-date-range-covers/#findComment-1417093 Share on other sites More sharing options...
searls03 Posted March 7, 2013 Author Share Posted March 7, 2013 it is returning no errors Quote Link to comment https://forums.phpfreaks.com/topic/275341-find-what-months-a-date-range-covers/#findComment-1417095 Share on other sites More sharing options...
teynon Posted March 7, 2013 Share Posted March 7, 2013 Do you get a blank white page? I'm gonna start offering this more often: https://chrome.google.com/remotedesktop If you want me to walk through what's going on and figure it out quick for you, install that in Chrome and send me a PM and i'll walk through it with you. Quote Link to comment https://forums.phpfreaks.com/topic/275341-find-what-months-a-date-range-covers/#findComment-1417097 Share on other sites More sharing options...
searls03 Posted March 7, 2013 Author Share Posted March 7, 2013 yes, I am getting a blank white page. I prefer not do do the remotedesktop...I am just sort of paranoid that way if you know what I mean Quote Link to comment https://forums.phpfreaks.com/topic/275341-find-what-months-a-date-range-covers/#findComment-1417098 Share on other sites More sharing options...
teynon Posted March 7, 2013 Share Posted March 7, 2013 Alright, then I suggest you read some of the articles in Jessica's signature. Debugging Your Code: Debugging your SQL | use print_r() or var_dump() | What does a php function do? | What does a term mean? | Don't see any errors? Quote Link to comment https://forums.phpfreaks.com/topic/275341-find-what-months-a-date-range-covers/#findComment-1417100 Share on other sites More sharing options...
searls03 Posted March 7, 2013 Author Share Posted March 7, 2013 ok Ill take a look Quote Link to comment https://forums.phpfreaks.com/topic/275341-find-what-months-a-date-range-covers/#findComment-1417101 Share on other sites More sharing options...
searls03 Posted March 7, 2013 Author Share Posted March 7, 2013 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 0Fatal 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 Quote Link to comment https://forums.phpfreaks.com/topic/275341-find-what-months-a-date-range-covers/#findComment-1417103 Share on other sites More sharing options...
searls03 Posted March 7, 2013 Author Share Posted March 7, 2013 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>'; Quote Link to comment https://forums.phpfreaks.com/topic/275341-find-what-months-a-date-range-covers/#findComment-1417104 Share on other sites More sharing options...
searls03 Posted March 7, 2013 Author Share Posted March 7, 2013 if you have any advice on how I could make this work with my registration system, that would be so awesome now that we got this working:) thanks for your help on getting it working Quote Link to comment https://forums.phpfreaks.com/topic/275341-find-what-months-a-date-range-covers/#findComment-1417105 Share on other sites More sharing options...
teynon Posted March 7, 2013 Share Posted March 7, 2013 Congrats. You can set that in your php.ini as well.http://php.net/manual/en/datetime.configuration.php Quote Link to comment https://forums.phpfreaks.com/topic/275341-find-what-months-a-date-range-covers/#findComment-1417106 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.