nielsv
nielsv

Reputation: 6830

Increment variable with number in variable + PHP

I have some variables like this:

$_8 = 0; $_9 = 0; $_10 = 0; $_11 = 0; $_12 = 0; $_13 = 0; $_14 = 0; $_15 = 0; $_16 = 0; $_17 = 0; $_18 = 0; $data = array();

Then I do the following:

$vistoday = $app['db']->fetchAll('SELECT `Datum Bezoek 1` FROM `psttodo-uit` WHERE CAST(`Datum Bezoek 1` AS DATE) = CURRENT_DATE AND PB = 1');

foreach($vistoday as $v){
    $date = strtotime($v['Datum Bezoek 1']);
    $hours = (int)date('h', $date);
    $minutes = (int)date('i', $date);

    if($minutes > 30)
    {
        $hours = $hours + 1;
        $count = ${"_" . $hours} + 1;
        $data[$hours] = $count;
    }
    else
    {
        $data[$hours] = ${"_" . $hours}+1;
    }
}

These are the dates I pull from the database:

When I output $data I get this:

array (size=2)
   8 => int 1
   9 => int 1

But Normally the value of key 9 should be 2. So he doesn't increment, he just adds 1 to nothing. Can somebody help me with this?

Upvotes: 0

Views: 85

Answers (1)

Alexey Prokhorov
Alexey Prokhorov

Reputation: 3941

Yoг don't need variables like $_8

foreach($vistoday as $v){
    $date = strtotime($v['Datum Bezoek 1']);
    $hours = (int)date('h', $date);
    $minutes = (int)date('i', $date);

    $hours = round($hours + $minutes / 61); // if $minutes > 30 ceil else floor
    $data[$hours] = isset($data[$hours]) ? $data[$hours] + 1 : 1;
}

Upvotes: 2

Related Questions