I have created a function that gives an array containing each month, starting from the carbon date supplied and the current date Ends. / P>
Although it is doing what it is doing, it looks terrible clearly my programming skills are not yet what they should be.
My code looks like this:
',' March, April, May, June, July, August, September, October, November, December. Public function getMonthListFromDate (carbon $ date) {$ monthArray = array (); $ Today = Carbon :: Today (); $ Current year = $ today- & gt; Copy () - & gt; Format ('Y'); $ CurrentMonth = strtolower ($ Today-> Copy () -> Format ('F')); $ StartYou = $ date- & gt; Copy () - & gt; Format ('Y'); $ StartMonth = strtolower ($ date-> Copy () -> Format ('F')); ($ I = $ startYear; $ i & lt; = $ currentYear; $ i ++) {foreach ($ this-> Months $ month as Indices => $ month) {if (($ monthIndex & Gt; = array_search ($ StartMount, $ this- & gt; months) & amp; $ i == $ startup} || ($ month indices & lt; = array_search ($ current month, $ this-> months ) & Amp; $ i == $ current year) ($ i! = $ Initials & $ i! = $ Current year)) {$ formatted matter index = ($ month index + 1) ; If ($ formattedmonthandex <10) {$ monthArray ['0' $ formatted fund index '-' $ I] = $ month '' $ I; } And {$ month old [$ formattedmonthindex '-' $ I] = $ month ''. $ I; }}}} Come back $ month; }}
And the result is:
array: 25 [▼ "03-2013" => "March 2013" "04-2013" => "April 2013" "05-2013" = & gt; "May 2013" "06-2013" = & gt; "June 2013" "07-2013" = & gt; "July 2013" "08-2013" = & gt; "August 2013" "09-2013" = & gt; "September 2013" "10-2013" = & gt; "October 2013" "11-2013" = & gt; "November 2013" "12-2013" = & gt; "December 2013" "01-2014" => "January 2014" "02-2014" => "February 2014" "03-2014" = & gt; "March 2014" "04-2014" => "April 2014" "05-2014" = & gt; "May 2014" "06-2014" = & gt; "June 2014" "07-2014" => "July 2014" "08-2014" = & gt; "August 2014" "09-2014" = & gt; "September 2014" "10-2014" = & gt; "October 2014" "11-2014" = & gt; "November 2014" "12-2014" = & gt; "December 2014" "01-2015" => "January 2015" "02-2015" = & gt; "February 2015" "03-2015" = & gt; "March 2015"]
Can someone help me improve this code?
Edit:
After the great tips I ended up with the following:
}
Thanks for the help guys!
With date time it can easily be achieved.
- Create a date item for each start and end date
- Set up 1 month interval
- 1 month between start date and end date Get a set of date between the interval of
Example:
public function getMonthListFromDate (Carbon $ date) { $ Start = new dateTime (); // today's date $ end = new date time ($ date-> toDateTimeString ()); // Create a datetime object from your carbon object $ interval = dateInterworld :: createFromDateString ('1 month'); // 1 month interval $ period = new date period ($ start, $ interval, $ end); // Get a set of passes of 2 months duration $ months = array (); Foreign Currency ($ period $ dt) {$ months [] = $ dt-> Format ("FY"); } $ Months return; }
See this in action:
No comments:
Post a Comment