4

I am trying to get the number of days from the first date to the second date, and then from the second date to the third date and so on. I have a array like this

$dates = array(
    2016 - 02 - 01,
    2016 - 03 - 01,
    2016 - 04 - 01,
    2016 - 05 - 01,
    2016 - 06 - 01,
    2016 - 07 - 01,
    2016 - 09 - 01,
    2016 - 11 - 01,
    2016 - 12 - 01,
    2017 - 01 - 01,
    2017 - 12 - 01
);

I want to get the number of days from 2016-02-01 to 2016-03-01, and then from 2016-03-01 to 2016-04-01 and so on, if you notice there are some gaps in the dates as in they do jump for more than 1 month. And I want it in a array like this

array()(
    [0] => 0,
    [1] => 30,
    [2] => 60
    //so on ...
) 

Here is how I am doing it but I am getting errors like uninitialized string offset and I guess the way I am doing is wrong most probably

public  function rangeDates()
{
    $dates = array(
        '2016-02-01',
        '2016-03-01',
        '2016-04-01',
        '2016-05-01',
        '2016-06-01',
        '2016-07-01',
        '2016-09-01',
        '2016-11-01',
        '2016-12-01',
        '2017-01-01',
        '2017-12-01'
    );
    $datez = array();
    $index = 0;
    $indexone = 1;
    foreach($dates as $date)
    {
        $datez = round(abs(strtotime($date[$index]) - strtotime($date[$indexone])) / 86400);
        $index++;
        $indexone++;
    }

    echo $datez;
}

The dates are in string format, extra information that I forgot to mention is that the days needs to get added, for example if we take just the years

array(11) (
  [0] => (int) 0
  [1] => (int) 365
  [2] => (int) 730
  [3] => (int) 1095
  [4] => (int) 1460
  [5] => (int) 1825
  [6] => (int) 2190
  [7] => (int) 2555
  [8] => (int) 2920
  [9] => (int) 3285
  [10] => (int) 3650
6
  • Please check this, and let me know is it okey for you or not. Just the first portion is all about you what you need. If you don't understand then i can make an answer for you. Commented Oct 5, 2016 at 8:58
  • eval.in/655456 Commented Oct 5, 2016 at 9:04
  • @FrayneKonok Sorry I guess i did not mention it properly or its hard to understand but what I want is the difference, but then getting added to the next month, so basically 0 , 30 , 60, 90, so its like the previous days is getting added to the next days. Hope I made sense Commented Oct 5, 2016 at 9:12
  • @Anant Sorry I guess i did not mention it properly or its hard to understand but what I want is the difference, but then getting added to the next month, so basically 0 , 30 , 60, 90, so its like the previous days is getting added to the next days. Hope I made sense Commented Oct 5, 2016 at 9:12
  • @MasnadNihit, If i make comment in your try then there are some points to note $datez =, You initialize this as array and use just like a string also at the last you echo it. Commented Oct 5, 2016 at 9:18

1 Answer 1

5

This is all about you need, Loop through the dates array then calculate the difference of the dates accordingly. Your loop will be run one short of the total array size, From the interval just call for the days and store it in an array.

$dates = array(
    "2016-02-01",
    "2016-03-01",
    "2016-04-01",
    "2016-05-01",
    "2016-06-01",
    "2016-07-01",
    "2016-09-01",
    "2016-11-01",
    "2016-12-01",
    "2017-01-01",
    "2017-12-01"
);

$datez = array();
$date = array();
$datez[] = 0;

for($i = 1; $i < count($dates) - 1; $i++){
    $start_date = $dates[$i-1];
    $end_Date = $dates[$i];

    $date1 = new DateTime($start_date);
    $date2 = new DateTime($end_Date);
    $interval = $date1->diff($date2);

    $date[]  = $interval->days;
    $datez[] = array_sum($date);
}

print_r($date);
print_r($datez);

Online Example

Sign up to request clarification or add additional context in comments.

9 Comments

Sorry I guess i did not mention it properly or its hard to understand but what I want is the difference, but then getting added to the next month, so basically 0 , 30 , 60, 90, so its like the previous days is getting added to the next days. Hope I made sense
If it is then you should not need all these.. Just a simple start date and end date difference will done your job.
I edited the question at the top, now it tells more detailed information at the end, could you please take a look
Your question still says what i did for you.
Konock, You are getting the difference between two dates, could you please help me out by adding the previous dates?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.