Joywork/import/helper/Date.php
2026-05-22 21:21:54 +03:00

115 lines
2.9 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
/**
* Хелпер для работы с датой и временем
*/
abstract class Date
{
const MYSQL_FORMAT = 'Y-m-d H:i:s';
const MYSQL_FORMAT_DATE = 'Y-m-d';
const DEADLINE_DONE = 'Сдан';
private static $endingPeriodFomat = '%d квартал %d';
public static function getQuarterByMonth($month)
{
assert('$month>0');
assert('$month<13');
return ceil(($month)/3);
}
/**
* 2030-06-20 => 2 квартал 2030
* @param string $ep ending period
* @param Clock|null $clock
* @return string
*/
public static function getFormatedEndingPeriod($ep, Clock $clock = null)
{
if ($clock === null) {
$clock = new RealtimeClock();
}
$curQuarter = self::getQuarterByMonth($clock->now()->format('m'));
$curYear = (int)$clock->now()->format('Y');
$parts = explode('-', $ep);
if (count($parts) != 3) {
return '';
}
$quarter = self::getQuarterByMonth((int)$parts[1]);
$year = (int)$parts[0];
if (
$year < $curYear || $year == $curYear
&& $quarter < $curQuarter
) {
return self::DEADLINE_DONE;
}
return sprintf(self::$endingPeriodFomat, $quarter, $year);
}
public static function parseDateTime($str)
{
return (int)strtotime($str);
}
public static function dateToMysql($time)
{
return date(self::MYSQL_FORMAT_DATE, $time);
}
public static function dateTimeToMysql($time)
{
return date(self::MYSQL_FORMAT, $time);
}
public static function getLastPeriodDate($ts)
{
$parts = explode('-', date('Y-m-d', $ts));
$i = floor($parts[1]/4)+1;
$lastDay = ($i == 1 && $i ==2) ? 30 : 31;
$month = 3*$i;
return $parts[0] . "-" . $month . "-". $lastDay;
}
public static function getDateForApi($date)
{
$ts = self::parseDateTime($date);
return date('d.m.Y', $ts);
}
public static function getListDeadlines($count = 10)
{
$lastDay = [
3 => 31, // март
6 => 30, // июнь
9 => 30, // сентябрь
12 => 31, // декабрь
];
// предыдущий квартал: сдан
$quarter = self::getQuarterByMonth((int)date('m')) -1;
$year = (int)date('Y');
if (!$quarter) {
$quarter = 4;
--$year;
}
$list = [];
while ($count--) {
$month = $quarter*3;
$ts = mktime(0, 0, 0, $month, $lastDay[$month], $year);
$formated = self::dateToMysql($ts);
$list[$ts] = self::getFormatedEndingPeriod($formated);
if (++$quarter > 4) {
$quarter = 1;
++$year;
}
}
return $list;
}
}