amer hendy
amer hendy

Reputation: 3

php Add numbers cumulatively from array to date

i have array with numbers , with not serialize keys. and have a date. how can i add dates with array values accumulative?

i used function with foreach{} and add days to date like that:

$nextDate[] = date("Y-m-d", strtotime('+'.$g.' day', strtotime($aDate)));

the data is:

$date='2019-01-01 00:00:00';
$days=[
    //id=>days
    '4'=>'1',
    '6'=>'2',
    '8'=>'5',
    '20'=>'2',
    '54'=>'6',
    '62'=>'4',
    '64'=>'1',
    '65'=>'1',
    '68'=>'1',
    '70'=>'1',
    '78'=>'20'
];

i want out but like that i want array become first id=date+current days , then next become id=prevdate+ current days

like that



$result=[
    //'4'=>'1',
    '4'=>'2019-01-02',// $date+1
    //'6'=>'2',
    '6'=>'2019-01-04',//previous result + 2
    //'8'=>'5',
    '8'=>'2019-01-09',//previous result + 5
    //'20'=>'2',
    '20'=>'2019-01-11',//previous result + 2
    //'54'=>'6',
    '54'=>'2019-01-17',//previous result + 2
    //'62'=>'4',
    '62'=>'2019-01-21',//previous result + 4
    //'64'=>'1',
    '64'=>'2019-01-22',//previous result + 1
    //'65'=>'1',
    '65'=>'2019-01-23',//previous result + 1
    //'68'=>'1',
    '68'=>'2019-01-24',//previous result + 1
    //'70'=>'1',
    '70'=>'2019-01-25',//previous result + 1
    //'78'=>'20'
    '78'=>'2019-02-14',//previous result + 20
];

Upvotes: 0

Views: 65

Answers (2)

jspit
jspit

Reputation: 7703

A simple foreach does it too.

The DateTime object $dateTime is changed by the modify method in each cycle. The following format method does not change this and only returns the desired date string for the new array value.

//given
$date='2019-01-01 00:00:00';
$days=[
    //id=>days
    '4'=>'1',
    '6'=>'2',
    '8'=>'5',
    '20'=>'2',
    '54'=>'6',
    '62'=>'4',
    '64'=>'1',
    '65'=>'1',
    '68'=>'1',
    '70'=>'1',
    '78'=>'20'
];

//calculation
$dateTime = new DateTime($date);
foreach($days as $key => $day){
  $days[$key] = $dateTime->modify($day." Days")->format('Y-m-d');
}

//Test output
echo "<pre>".var_export($days,true)."</pre>";

Output

array (
  4 => '2019-01-02',
  6 => '2019-01-04',
  8 => '2019-01-09',
  20 => '2019-01-11',
  54 => '2019-01-17',
  62 => '2019-01-21',
  64 => '2019-01-22',
  65 => '2019-01-23',
  68 => '2019-01-24',
  70 => '2019-01-25',
  78 => '2019-02-14',
)

Note: Outputs via var_export can be copied and simply used as php source code for further testing.

Upvotes: 0

blupointmedia
blupointmedia

Reputation: 604

Here you go. I am passing in the date object, which is by reference so it modifies the object as we add to it.

$date = new DateTime('2019-01-01 00:00:00');
$days=[
    //id=>days
    '4'=>'1',
    '6'=>'2',
    '8'=>'5',
    '20'=>'2',
    '54'=>'6',
    '62'=>'4',
    '64'=>'1',
    '65'=>'1',
    '68'=>'1',
    '70'=>'1',
    '78'=>'20'
];

$result = array_map(function($days) use ($date) {
  $date->add(new DateInterval('P' . $days . 'D'));
  return  $date->format('Y-m-d');
}, $days);

print_r($result);

Upvotes: 1

Related Questions