Exemplo n.º 1
0
 protected static function getCalendar($month, $year)
 {
     //В коде может использоваться другой пакет
     if (CoreLib\Loader::issetPackage('dates')) {
         //Если пакет установлен, подключаем его
         CoreLib\Loader::IncludePackage('dates');
     }
     //$month = 2; $year = 2017; //Для теста
     $arCalendar = array();
     $dateHelper = new CoreLib\DateHelper();
     $arCalendar["INFO"]["MONTH"] = $month;
     $arCalendar["INFO"]["YEAR"] = $year;
     //Получаем timestamp на первое число заданного месяца и года
     $arCalendar["INFO"]["START_DATE"] = "1." . $month . "." . $year;
     $arCalendar["INFO"]["START_TIMESTAMP"] = $dateHelper->getDateTimestamp($arCalendar["INFO"]["START_DATE"]);
     //Получаем количество дней в месяце
     $arCalendar["INFO"]["NUMBER_OF_DAYS"] = intval(date("t", $arCalendar["INFO"]["START_TIMESTAMP"]));
     //Получаем timestamp на последнее число заданного месяца и года
     $arCalendar["INFO"]["LAST_DATE"] = $arCalendar["INFO"]["NUMBER_OF_DAYS"] . "." . $month . "." . $year;
     $arCalendar["INFO"]["LAST_TIMESTAMP"] = $dateHelper->getDateTimestamp($arCalendar["INFO"]["LAST_DATE"]);
     //Получаем день недели для 1го числа месяца
     $arCalendar["INFO"]["START_DAY_OF_WEEK"] = intval(date("w", $arCalendar["INFO"]["START_TIMESTAMP"]));
     if ($arCalendar["INFO"]["START_DAY_OF_WEEK"] == 0) {
         $arCalendar["INFO"]["START_DAY_OF_WEEK"] = 7;
     }
     //Получаем день недели для последнего числа месяца
     $arCalendar["INFO"]["LAST_DAY_OF_WEEK"] = intval(date("w", $arCalendar["INFO"]["LAST_TIMESTAMP"]));
     if ($arCalendar["INFO"]["LAST_DAY_OF_WEEK"] == 0) {
         $arCalendar["INFO"]["LAST_DAY_OF_WEEK"] = 7;
     }
     //Если 1е число не понедельник - нужно получить несколько дней предыдущего месяца
     if ($arCalendar["INFO"]["START_DAY_OF_WEEK"] != 1) {
         $arCalendar["INFO"]["DAY_PREV_MONTH"] = $arCalendar["INFO"]["START_DAY_OF_WEEK"] - 1;
         $arCalendar["INFO"]["FIRST_DAY_PREV_MONTH"] = $dateHelper->strToTime($arCalendar["INFO"]["START_DATE"], "-" . $arCalendar["INFO"]["DAY_PREV_MONTH"] . " day");
         list($arCalendar["INFO"]["PREV_MONTH_DAY"], $arCalendar["INFO"]["PREV_MONTH_MONTH"], $arCalendar["INFO"]["PREV_MONTH_YEAR"]) = explode(".", $arCalendar["INFO"]["FIRST_DAY_PREV_MONTH"]);
         for ($i = 0; $i < $arCalendar["INFO"]["DAY_PREV_MONTH"]; $i++) {
             if ($i == 0) {
                 $dayOfWeek = date("w", $dateHelper->getDateTimestamp(intval($arCalendar["INFO"]["PREV_MONTH_DAY"]) . "." . intval($arCalendar["INFO"]["PREV_MONTH_MONTH"]) . "." . intval($arCalendar["INFO"]["PREV_MONTH_YEAR"])));
                 if ($dayOfWeek == 0) {
                     $dayOfWeek = 7;
                 }
                 //Использование пакета "Даты"
                 if (CoreLib\Loader::issetPackage('dates')) {
                     $isDayOff = \MSergeev\Packages\Dates\Lib\WorkCalendar::isDayOff(intval($arCalendar["INFO"]["PREV_MONTH_DAY"]) . "." . intval($arCalendar["INFO"]["PREV_MONTH_MONTH"]) . "." . intval($arCalendar["INFO"]["PREV_MONTH_YEAR"]));
                     if ($isDayOff) {
                         $dayOfWeek = 6;
                     } else {
                         $dayOfWeek = 1;
                     }
                 }
                 $arCalendar["PREV_MONTH"][intval($arCalendar["INFO"]["PREV_MONTH_DAY"]) . "." . intval($arCalendar["INFO"]["PREV_MONTH_MONTH"]) . "." . intval($arCalendar["INFO"]["PREV_MONTH_YEAR"])] = array("TEXT" => intval($arCalendar["INFO"]["PREV_MONTH_DAY"]) . " " . self::getMonthName(intval($arCalendar["INFO"]["PREV_MONTH_MONTH"])), "DAY_OF_WEEK" => $dayOfWeek);
             } else {
                 $dayOfWeek = date("w", $dateHelper->getDateTimestamp(intval($arCalendar["INFO"]["PREV_MONTH_DAY"]) + $i . "." . intval($arCalendar["INFO"]["PREV_MONTH_MONTH"]) . "." . intval($arCalendar["INFO"]["PREV_MONTH_YEAR"])));
                 if ($dayOfWeek == 0) {
                     $dayOfWeek = 7;
                 }
                 //Использование пакета "Даты"
                 if (CoreLib\Loader::issetPackage('dates')) {
                     $isDayOff = \MSergeev\Packages\Dates\Lib\WorkCalendar::isDayOff(intval($arCalendar["INFO"]["PREV_MONTH_DAY"]) + $i . "." . intval($arCalendar["INFO"]["PREV_MONTH_MONTH"]) . "." . intval($arCalendar["INFO"]["PREV_MONTH_YEAR"]));
                     if ($isDayOff) {
                         $dayOfWeek = 6;
                     } else {
                         $dayOfWeek = 1;
                     }
                 }
                 $arCalendar["PREV_MONTH"][intval($arCalendar["INFO"]["PREV_MONTH_DAY"]) + $i . "." . intval($arCalendar["INFO"]["PREV_MONTH_MONTH"]) . "." . intval($arCalendar["INFO"]["PREV_MONTH_YEAR"])] = array("TEXT" => intval($arCalendar["INFO"]["PREV_MONTH_DAY"]) + $i, "DAY_OF_WEEK" => $dayOfWeek);
             }
         }
     }
     //Вносим информацию по заданному месяцу
     for ($i = 0; $i < $arCalendar["INFO"]["NUMBER_OF_DAYS"]; $i++) {
         if ($i == 0) {
             $dayOfWeek = date("w", $dateHelper->getDateTimestamp($i + 1 . "." . $month . "." . $year));
             if ($dayOfWeek == 0) {
                 $dayOfWeek = 7;
             }
             //Использование пакета "Даты"
             if (CoreLib\Loader::issetPackage('dates')) {
                 $isDayOff = \MSergeev\Packages\Dates\Lib\WorkCalendar::isDayOff($i + 1 . "." . $month . "." . $year);
                 if ($isDayOff) {
                     $dayOfWeek = 6;
                 } else {
                     $dayOfWeek = 1;
                 }
             }
             $arCalendar["NOW_MONTH"][$i + 1 . "." . $month . "." . $year] = array("TEXT" => $i + 1 . " " . self::getMonthName($month), "DAY_OF_WEEK" => $dayOfWeek);
         } else {
             $dayOfWeek = date("w", $dateHelper->getDateTimestamp($i + 1 . "." . $month . "." . $year));
             if ($dayOfWeek == 0) {
                 $dayOfWeek = 7;
             }
             //Использование пакета "Даты"
             if (CoreLib\Loader::issetPackage('dates')) {
                 $isDayOff = \MSergeev\Packages\Dates\Lib\WorkCalendar::isDayOff($i + 1 . "." . $month . "." . $year);
                 if ($isDayOff) {
                     $dayOfWeek = 6;
                 } else {
                     $dayOfWeek = 1;
                 }
             }
             $arCalendar["NOW_MONTH"][$i + 1 . "." . $month . "." . $year] = array("TEXT" => $i + 1, "DAY_OF_WEEK" => $dayOfWeek);
         }
     }
     //Если последний день заданного месяца не воскресение - нужно добавить несколько дней следующего месяца
     if ($arCalendar["INFO"]["LAST_DAY_OF_WEEK"] != 7) {
         $arCalendar["INFO"]["FIRST_DAY_NEXT_MONTH"] = $dateHelper->strToTime($arCalendar["INFO"]["LAST_DATE"], "+1 day");
         list($arCalendar["INFO"]["NEXT_MONTH_DAY"], $arCalendar["INFO"]["NEXT_MONTH_MONTH"], $arCalendar["INFO"]["NEXT_MONTH_YEAR"]) = explode(".", $arCalendar["INFO"]["FIRST_DAY_NEXT_MONTH"]);
         $arCalendar["INFO"]["DAY_NEXT_MONTH"] = 7 - $arCalendar["INFO"]["LAST_DAY_OF_WEEK"];
         for ($i = 0; $i < $arCalendar["INFO"]["DAY_NEXT_MONTH"]; $i++) {
             if ($i == 0) {
                 $dayOfWeek = date("w", $dateHelper->getDateTimestamp($i + 1 . "." . intval($arCalendar["INFO"]["NEXT_MONTH_MONTH"]) . "." . intval($arCalendar["INFO"]["NEXT_MONTH_YEAR"])));
                 if ($dayOfWeek == 0) {
                     $dayOfWeek = 7;
                 }
                 //Использование пакета "Даты"
                 if (CoreLib\Loader::issetPackage('dates')) {
                     $isDayOff = \MSergeev\Packages\Dates\Lib\WorkCalendar::isDayOff($i + 1 . "." . intval($arCalendar["INFO"]["NEXT_MONTH_MONTH"]) . "." . intval($arCalendar["INFO"]["NEXT_MONTH_YEAR"]));
                     if ($isDayOff) {
                         $dayOfWeek = 6;
                     } else {
                         $dayOfWeek = 1;
                     }
                 }
                 $arCalendar["NEXT_MONTH"][$i + 1 . "." . intval($arCalendar["INFO"]["NEXT_MONTH_MONTH"]) . "." . intval($arCalendar["INFO"]["NEXT_MONTH_YEAR"])] = array("TEXT" => $i + 1 . " " . self::getMonthName(intval($arCalendar["INFO"]["NEXT_MONTH_MONTH"])), "DAY_OF_WEEK" => $dayOfWeek);
             } else {
                 $dayOfWeek = date("w", $dateHelper->getDateTimestamp($i + 1 . "." . intval($arCalendar["INFO"]["NEXT_MONTH_MONTH"]) . "." . intval($arCalendar["INFO"]["NEXT_MONTH_YEAR"])));
                 if ($dayOfWeek == 0) {
                     $dayOfWeek = 7;
                 }
                 //Использование пакета "Даты"
                 if (CoreLib\Loader::issetPackage('dates')) {
                     $isDayOff = \MSergeev\Packages\Dates\Lib\WorkCalendar::isDayOff($i + 1 . "." . intval($arCalendar["INFO"]["NEXT_MONTH_MONTH"]) . "." . intval($arCalendar["INFO"]["NEXT_MONTH_YEAR"]));
                     if ($isDayOff) {
                         $dayOfWeek = 6;
                     } else {
                         $dayOfWeek = 1;
                     }
                 }
                 $arCalendar["NEXT_MONTH"][$i + 1 . "." . intval($arCalendar["INFO"]["NEXT_MONTH_MONTH"]) . "." . intval($arCalendar["INFO"]["NEXT_MONTH_YEAR"])] = array("TEXT" => $i + 1, "DAY_OF_WEEK" => $dayOfWeek);
             }
         }
     }
     return $arCalendar;
 }
Exemplo n.º 2
0
 protected static function parseSunRiseSet($city, $arWeather)
 {
     $dateHelper = new DateHelper();
     list($sunRiseDate, $sunRiseTime) = explode('T', $arWeather['sun']['@attributes']['rise']);
     $sunRiseDate = $dateHelper->convertDateFromDB($sunRiseDate);
     $sunRiseTime = explode(':', $sunRiseTime);
     $sunRiseTime[0] += $city['UTC_HOUR'];
     if ($sunRiseTime[0] > 23) {
         $sunRiseTime[0] -= 24;
         $sunRiseDate = $dateHelper->strToTime($sunRiseDate);
     }
     list($sunSetDate, $sunSetTime) = explode('T', $arWeather['sun']['@attributes']['set']);
     $sunSetDate = $dateHelper->convertDateFromDB($sunSetDate);
     $sunSetTime = explode(':', $sunSetTime);
     $sunSetTime[0] += $city['UTC_HOUR'];
     if ($sunSetTime[0] > 23) {
         $sunSetTime[0] -= 24;
         $sunSetDate = $dateHelper->strToTime($sunSetDate);
     }
     $sunRiseMin = $sunRiseTime[0] * 60 + $sunRiseTime[1];
     $sunSetMin = $sunSetTime[0] * 60 + $sunSetTime[1];
     if ($sunSetMin >= $sunRiseMin) {
         $sunDayMin = $sunSetMin - $sunRiseMin;
     } else {
         $sunDayMin = $sunSetMin + 24 * 60 - $sunRiseMin;
     }
     $sunDayHour = floor($sunDayMin / 60);
     $sunDayMin -= $sunDayHour * 60;
     $arSunRise = SunTable::getList(array('filter' => array('CITY_ID' => $city['ID'], 'DATE' => $sunRiseDate)));
     $arInsertUpdate = array('CITY_ID' => $city['ID'], 'DATE' => $sunRiseDate, 'SUNRISE' => $sunRiseTime[0] . ':' . $sunRiseTime[1] . ':' . $sunRiseTime[2], 'SUNSET' => $sunSetTime[0] . ':' . $sunSetTime[1] . ':' . $sunSetTime[2], 'DAYTIME' => $sunDayHour . ':' . $sunDayMin . ':00');
     if ($arSunRise) {
         //Update
         $query = new Query('update');
         $query->setUpdateParams($arInsertUpdate, $arSunRise[0]['ID'], SunTable::getTableName(), SunTable::getMapArray());
         $res = $query->exec();
         if ($res->getResult()) {
             return $res->getInsertId();
         } else {
             return false;
         }
     } else {
         //Insert
         $query = new Query('insert');
         $query->setInsertParams($arInsertUpdate, SunTable::getTableName(), SunTable::getMapArray());
         $res = $query->exec();
         if ($res->getResult()) {
             return $res->getAffectedRows();
         } else {
             return false;
         }
     }
 }