function isWorkTime(string $timeFrom, string $timeTo, ?string $currentTime = null): bool
{
$t1 = strtotime($timeFrom);
$t2 = strtotime($timeTo);
$now = $currentTime === null ? time() : strtotime($currentTime);
if ($t2 < $t1) {
if ($t1 > $now) {
$t1 -= 86400; // минус одни сутки
} else {
$t2 += 86400; // плюс одни сутки
}
}
return $t1 <= $now && $now < $t2;
}
var_export([
'=== 12:00 - 13:00 (1 hour) ===',
'1. 11:59' => isWorkTime('12:00', '13:00', '11:59'),
'1. 12:00' => isWorkTime('12:00', '13:00', '12:00'),
'1. 12:01' => isWorkTime('12:00', '13:00', '12:01'),
'1. 12:59' => isWorkTime('12:00', '13:00', '12:59'),
'1. 13:00' => isWorkTime('12:00', '13:00', '13:00'),
'1. 13:01' => isWorkTime('12:00', '13:00', '13:01'),
'1. 00:00' => isWorkTime('12:00', '13:00', '00:00'),
'=== 00:00 - 12:00, 13:00 - 00:00 (23 hours) ===',
'2. 11:59' => isWorkTime('13:00', '12:00', '11:59'),
'2. 12:00' => isWorkTime('13:00', '12:00', '12:00'),
'2. 12:01' => isWorkTime('13:00', '12:00', '12:01'),
'2. 12:59' => isWorkTime('13:00', '12:00', '12:59'),
'2. 13:00' => isWorkTime('13:00', '12:00', '13:00'),
'2. 13:01' => isWorkTime('13:00', '12:00', '13:01'),
'2. 00:00' => isWorkTime('13:00', '12:00', '00:00'),
], false);
$doc = new DOMDocument();
$doc->loadHTML($htmlAsString);
$links = $doc->getElementsByTagName('a');
function filterByHref(DOMNodeList $anchors, string $hrefRegex): array {
$links = [];
/** @var DOMElement $elm */
foreach ($anchors as $elm) {
if (! $elm->hasAttribute('href')) {
continue;
}
$link = $elm->getAttribute('href');
if (preg_match($hrefRegex, $link)) {
$links[] = $link;
}
}
return $links;
}
var_export([
'cnt' => $links->length,
'goo' => filterByHref($links, '|^https://google.com\b|'),
'link' => filterByHref($links, '|link1|i'),
], false);
$cities = [];
while ($ob = $res->GetNextElement()) {
$cityName = ...;
$image = ...;
$cities[$city][] = $image;
}
foreach ($cities as $cityName => $images) {
...
foreach ($images as $image) {
...
}
...
}
function isItemExists(iterable $source, $expectedKey, $expectedValue): bool
{
$iterator = new RecursiveIteratorIterator(
new RecursiveArrayIterator($source, RecursiveArrayIterator::CHILD_ARRAYS_ONLY),
RecursiveIteratorIterator::LEAVES_ONLY
);
foreach ($iterator as $itemKey => $itemValue) {
if ($expectedKey === $itemKey && $expectedValue === $itemValue) {
return true;
}
}
return false;
}
$firstIpAsString = '77.88.0.0';
$lastIpAsString = '77.88.0.18';
$ipAsString = '77.88.0.111';
$firstIp = ip2long($firstIpAsString);
$lastIp = ip2long($lastIpAsString);
$ip = ip2long($ipAsString);
var_export([
'first' => $firstIp,
'last' => $lastIp,
'ip' => $ip,
'in range?' => $firstIp <= $ip && $ip <= $lastIp,
], false);
array_map
или array_walk
.$limit = 8;
// VAR 1
$processedItems = \array_map(function ($value) use ($limit) {
$subHours = 0;
$hours = $value['hours'];
if ($hours > $limit) {
$subHours = $hours - $limit;
$hours = $limit;
}
return [
'hours' => $hours,
'sub_hours' => $subHours,
];
}, $items);
// VAR 2
\array_walk($items, function (&$value) use ($limit) {
$hours = $value['hours'];
if ($hours > $limit) {
$value['hours'] = $limit;
$value['sub_hours'] = $hours - $limit;
}
});
$x = <<<XML
<Объект Нпп="1234" Тип="ДокументСсылка.РеализацияУслуг" ИмяПравила="РеализацияУслуг"><Ссылка Нпп="2142">
<Свойство Имя="{УникальныйИдентификатор}" Тип="Строка">
<Значение>LIMB-001628</Значение>
</Свойство>
<Свойство Имя="Дата" Тип="Дата">
<Значение>2019-02-28T17:24:12</Значение>
</Свойство>
</Ссылка>
</Объект>
XML;
$arr = \json_decode($a, true);
$reducer = function ($carry, $item): float {
if ('qw_wallet_rub' === $item['alias'] ?? null) {
return $carry + $item['balance']['amount'] ?? 0.0;
}
return (float)$carry;
};
$result = \array_reduce($arr['accounts'], $reducer);
/**
* @param \DateTimeImmutable $dateFrom
* @param \DateTimeImmutable $dateTo
*
* @return \DateTimeImmutable[]
* @throws \Exception
*/
function daysRange(\DateTimeImmutable $dateFrom, \DateTimeImmutable $dateTo) : iterable
{
$iter = clone $dateFrom;
while ($iter <= $dateTo) {
yield $iter;
$iter = $iter->add(new \DateInterval('P1D'));
}
}
$days = daysRange(
new \DateTimeImmutable('2020-01-25'),
new \DateTimeImmutable('2020-03-01')
);
foreach ($days as $day) {
echo sprintf("%s\n", $day->format('Y-m-d'));
}
$data = json_decode('{qwe}', true);
if (JSON_ERROR_NONE !== json_last_error()) {
throw new \RuntimeException(sprintf(
'JSON parsing error: `%s`',
json_last_error_msg()
));
}
var_export($data, false);
$data = json_decode('null', true) ?? [];