3

We already created Weekly array

Like

[1] => Array ( [start] => 2017-01-01 [end] => 2017-01-07 )

But we need monthly array start date and end date of month

Just Like this

Array
   (
       [1] => Array
           (
               [start] => 2017-01-01
               [end] => 2017-02-01
           )

       [2] => Array
           (
               [start] => 2017-02-01
               [end] => 2017-03-01
           )

       [3] => Array
           (
               [start] => 2017-03-01
               [end] => 2017-04-01
           )

       [4] => Array
           (
               [start] => 2017-04-01
               [end] => 2017-05-01
           )

       [5] => Array
           (
               [start] => 2017-05-01
               [end] => 2017-06-01
           )

   )
3
  • 1
    Post your code.. let's see what you have tried..
    – Zedex7
    Aug 22, 2017 at 6:25
  • If you already did the array for weeks, why would you have problem with months? Aug 22, 2017 at 6:25
  • There is 7 days are fix in a week but month days are not fix each time
    – Raj Samani
    Aug 22, 2017 at 6:27

1 Answer 1

2

Here we are using DateTime and DateInterval for achieving expected output.

Try this code snippet here

<?php
ini_set('display_errors', 1);

$startDate="2017-01-01";
$endDate="2017-06-01";

$dates=array();
while($startDate!=$endDate)
{
    $monthEndDate=new DateTime($startDate);
    $monthEndDate->add(new DateInterval("P1M"));//adding one month each time we iterate
    $dates[]=array("start" => $startDate,
                   "end" => $monthEndDate->format("Y-m-d"));
    $startDate=$monthEndDate->format("Y-m-d");//changing start date
}
print_r($dates);
0

Not the answer you're looking for? Browse other questions tagged or ask your own question.