1. PHP / Говнокод #21778

    −15

    1. 01
    2. 02
    3. 03
    4. 04
    5. 05
    6. 06
    7. 07
    8. 08
    9. 09
    10. 10
    11. 11
    12. 12
    13. 13
    14. 14
    15. 15
    16. 16
    17. 17
    18. 18
    19. 19
    20. 20
    21. 21
    22. 22
    23. 23
    24. 24
    25. 25
    26. 26
    27. 27
    28. 28
    29. 29
    30. 30
    31. 31
    32. 32
    33. 33
    34. 34
    35. 35
    36. 36
    37. 37
    38. 38
    $start = "2016-11-29";
    $end = "2016-12-31";
    $nnomer = "lux";
    
    const SQL_GET_MENU_NOMERPRICE = '
    SELECT `1`, `2`, `3`, `4`, `5`, `6`, `7`, `8`, `9`, `10`, `11`, `12` FROM nomerprice WHERE name = :name
    ';
    
    $pdo = new PDO($dsn, $user, $password);
    $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    $stmt = $pdo->prepare(SQL_GET_MENU_NOMERPRICE);
    $res = $stmt->execute([':name' => $nnomer]);
    $nprice = json_encode(array_pop($stmt->fetchAll(PDO::FETCH_OBJ)), JSON_NUMERIC_CHECK);
    // $nprice сейчас равен {"1":1500,"2":1000,"3":2000,"4":1000,"5":1500,"6":2000,"7":1500,"8":2000,"9":1500,"10":3000,"11":2000,"12":1500}
    
    if (!empty($start) && !empty($end)) {
        $startDate = new DateTime($start);
        $endDate = new DateTime($end);
        $dates = array($startDate, $endDate);
     
        $pricesPerMonth = array(
            1 => 1500, 2 => 2000, 3 => 1500, 4 => 2000, 5 => 1500,
            6 => 2000, 7 => 1500, 8 => 2000, 9 => 1500, 10 => 2000,
            11 => 1500, 12 => 2000
        );
    
        $price = 0;
        for ($i = $startDate->getTimestamp(); $i <= $endDate->getTimestamp(); $i += 86400) {
            $newDate = new DateTime();
            $newDate->setTimestamp($i);
            $m = $newDate->format('m');
            $m = intval($m);
            if ($pricesPerMonth[$m]) {
                $price += $pricesPerMonth[$m];
            }
        }
        var_dump($price);
    }

    https://toster.ru/q/376912

    Запостил: riot26, 04 Декабря 2016

    Комментарии (2) RSS

    Добавить комментарий