简体   繁体   中英

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:

  • 2014-02-27 08:25:34
  • 2014-02-27 08:50:34
  • 2014-02-27 08:55:34

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?

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;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM