Jump to content

Recommended Posts

How do I get a series of date ranges by a start date and end date.

For example

Start Date - 02/14/06

End Date - 05/04/08

I need to take that start date and end date, then generate an array that has every month in between.

Array[1] - 2-14-06

Array[2] - 3-14-06

Array[3] - 4-14-06

All the way up until the end date.

However the start date and end date can be however far about it wants, and I need to be able to accurately grab the date ranges.  If possible I need to be able to also count how many ranges there are too.

If you can help I would be greatly appreciative.

Link to comment
https://forums.phpfreaks.com/topic/86144-solved-date-ranges/
Share on other sites

I have created the following function but it doesn't have the exact affect, but it's as close as I have gotten so far.

	// get dates into start and end
	$startdate = $prod_codes[0]['award_date'];
	$enddate = $prod_codes[0]['end_of_term'];

	$range = array(); // empty array to trap ranges

	if (is_string($startdate) === true) {
		$start = strtotime($startdate);
	}
	if (is_string($enddate) === true ) {
		$end = strtotime($enddate);
	}
	if ($start > $end) 
		return createDateRangeArray($end, $start);

	do {
		$range[] = date('Y-m-d', $start);
		$start = strtotime("+ 1 day", $start);
	}
	while($start < $end);

	pr($range);

 

This creates an array and output's it, but not creating it like I want.  Here is the array result.

Array

(

    [0] => 2004-01-01

    [1] => 2004-01-02

    [2] => 2004-01-03

    [3] => 2004-01-04

    [4] => 2004-01-05

    [5] => 2004-01-06

    [6] => 2004-01-07

    [7] => 2004-01-08

    [8] => 2004-01-09

    [9] => 2004-01-10

    [10] => 2004-01-11

    [11] => 2004-01-12

    [12] => 2004-01-13

    [13] => 2004-01-14

    [14] => 2004-01-15

    [15] => 2004-01-16

    [16] => 2004-01-17

    [17] => 2004-01-18

    [18] => 2004-01-19

    [19] => 2004-01-20

    [20] => 2004-01-21

    [21] => 2004-01-22

    [22] => 2004-01-23

    [23] => 2004-01-24

    [24] => 2004-01-25

    [25] => 2004-01-26

    [26] => 2004-01-27

    [27] => 2004-01-28

    [28] => 2004-01-29

    [29] => 2004-01-30

    [30] => 2004-01-31

    [31] => 2004-02-01

    [32] => 2004-02-02

    [33] => 2004-02-03

    [34] => 2004-02-04

    [35] => 2004-02-05

    [36] => 2004-02-06

    [37] => 2004-02-07

    [38] => 2004-02-08

    [39] => 2004-02-09

    [40] => 2004-02-10

    [41] => 2004-02-11

    [42] => 2004-02-12

    [43] => 2004-02-13

    [44] => 2004-02-14

    [45] => 2004-02-15

    [46] => 2004-02-16

    [47] => 2004-02-17

    [48] => 2004-02-18

    [49] => 2004-02-19

    [50] => 2004-02-20

    [51] => 2004-02-21

    [52] => 2004-02-22

    [53] => 2004-02-23

    [54] => 2004-02-24

    [55] => 2004-02-25

    [56] => 2004-02-26

    [57] => 2004-02-27

    [58] => 2004-02-28

    [59] => 2004-02-29

    [60] => 2004-03-01

    [61] => 2004-03-02

    [62] => 2004-03-03

    [63] => 2004-03-04

    [64] => 2004-03-05

    [65] => 2004-03-06

    [66] => 2004-03-07

    [67] => 2004-03-08

    [68] => 2004-03-09

    [69] => 2004-03-10

    [70] => 2004-03-11

    [71] => 2004-03-12

    [72] => 2004-03-13

    [73] => 2004-03-14

    [74] => 2004-03-15

    [75] => 2004-03-16

    [76] => 2004-03-17

    [77] => 2004-03-18

    [78] => 2004-03-19

    [79] => 2004-03-20

    [80] => 2004-03-21

    [81] => 2004-03-22

    [82] => 2004-03-23

    [83] => 2004-03-24

    [84] => 2004-03-25

    [85] => 2004-03-26

    [86] => 2004-03-27

    [87] => 2004-03-28

    [88] => 2004-03-29

    [89] => 2004-03-30

    [90] => 2004-03-31

    [91] => 2004-04-01

    [92] => 2004-04-02

    [93] => 2004-04-03

    [94] => 2004-04-04

    [95] => 2004-04-05

    [96] => 2004-04-06

    [97] => 2004-04-07

    [98] => 2004-04-08

    [99] => 2004-04-09

    [100] => 2004-04-10

    [101] => 2004-04-11

    [102] => 2004-04-12

    [103] => 2004-04-13

    [104] => 2004-04-14

    [105] => 2004-04-15

    [106] => 2004-04-16

    [107] => 2004-04-17

    [108] => 2004-04-18

    [109] => 2004-04-19

    [110] => 2004-04-20

    [111] => 2004-04-21

    [112] => 2004-04-22

    [113] => 2004-04-23

    [114] => 2004-04-24

    [115] => 2004-04-25

    [116] => 2004-04-26

    [117] => 2004-04-27

    [118] => 2004-04-28

    [119] => 2004-04-29

    [120] => 2004-04-30

    [121] => 2004-05-01

    [122] => 2004-05-02

    [123] => 2004-05-03

    [124] => 2004-05-04

 

There are over 500 in that array but you can get the point.  It's creating an array using the "DAY" as what to create by.  I need it to do the SAME theory it is using now except instead of using the DAY to make the range, it would use the month, for example.

01-05-07

02-05-07

03-05-07

Just as an example, to do it by month, instead of by day.

Any advice on what I can do to this function to make it do what I am wanting it to do?

Link to comment
https://forums.phpfreaks.com/topic/86144-solved-date-ranges/#findComment-439978
Share on other sites

Here's my take:

<?php
$start_date = $_GET['start'];
$end_date = $_GET['end'];
$tst = strtotime($start_date);
$end = strtotime($end_date);
$dr = array();
while ($tst <= $end) {
        $dr[] = date('Y-m-d',$tst);
        $tst = strtotime('+1 month',$tst);
}
echo '<pre>' . print_r($dr,true) . '</pre>';
?>

 

Ken

 

Link to comment
https://forums.phpfreaks.com/topic/86144-solved-date-ranges/#findComment-439999
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.