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) {
...
}
...
}
select t2.birthday,
date_add(
date_add(
makedate(b_year, 1),
interval month(t2.birthday) - 1 month
),
interval
if(month(t2.birthday) = 2 and day(t2.birthday) = 29 and b_year % 4 <> 0,
28,
day(t2.birthday)
)
- 1 day
) nearest_birthday
from (
select t.birthday,
year(@now) +
if(100 * month(t.birthday) + day(t.birthday) < 100 * month(@now) + day(@now),
1,
0
) b_year
from t,
(select @now := '2012-12-14') vars -- <<== CURRENT DATE
) t2
having timestampdiff(day, @now, nearest_birthday) <= 85 -- <<== WINDOW
order by nearest_birthday
;
drop table if exists t;
create table t (
birthday date not null
);
insert into t(birthday) values('1980-01-01');
insert into t(birthday) values('1980-01-15');
insert into t(birthday) values('1981-02-01');
insert into t(birthday) values('1981-02-15');
insert into t(birthday) values('2000-02-28');
insert into t(birthday) values('2000-02-29');
insert into t(birthday) values('1982-03-01');
insert into t(birthday) values('1982-03-15');
insert into t(birthday) values('1983-04-01');
insert into t(birthday) values('1983-04-15');
insert into t(birthday) values('1983-05-01');
insert into t(birthday) values('1983-05-15');
insert into t(birthday) values('1983-06-01');
insert into t(birthday) values('1983-06-15');
insert into t(birthday) values('1983-07-01');
insert into t(birthday) values('1983-07-15');
insert into t(birthday) values('1983-08-01');
insert into t(birthday) values('1983-08-15');
insert into t(birthday) values('1983-09-01');
insert into t(birthday) values('1983-09-15');
insert into t(birthday) values('1983-10-01');
insert into t(birthday) values('1983-10-15');
insert into t(birthday) values('1983-11-01');
insert into t(birthday) values('1983-11-15');
insert into t(birthday) values('1983-12-01');
insert into t(birthday) values('1983-12-15');
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) ?? [];
:ia $h hhhhh hhhhh
" нормально отрабатывает. Заменяет "$h
" на "hhhhh hhhhh
".:version
VIM - Vi IMproved 7.3 (2010 Aug 15, compiled Oct 18 2010 15:14:47)
Заплатки: 1-29