function diff(string $from, string $to): string
{
return date_diff(new \DateTime($from), new \DateTime($to))->format('%h:%i');
}
$arHistory['DATE_FROM'] = '2018-05-03 00:00:00';
$arHistory['DATE_TO'] = '2018-05-03 23:59:59';
echo diff($arHistory['DATE_FROM'], $arHistory['DATE_TO']);
function diff(string $from, string $to): string
{
$start = (new \DateTime($from))->getTimestamp();
$end = (new \DateTime($to))->getTimestamp();
$result = round(abs($start - $end) / 60 / 60, 4);
$hours = (int)$result;
$minutes = (int)(($result - $hours) * 60);
return $hours . ':' . $minutes;
}
$arHistory['DATE_FROM'] = '2018-05-03 00:00:00';
$arHistory['DATE_TO'] = '2018-05-03 23:59:59';
echo diff($arHistory['DATE_FROM'], $arHistory['DATE_TO']);
function diff(string $from, string $to): string
{
return (new \DateTime($from))->diff(new \DateTime($to))->format('%h:%i');
}
$arHistory['DATE_FROM'] = '2018-05-03 00:00:00';
$arHistory['DATE_TO'] = '2018-05-03 23:59:59';
echo diff($arHistory['DATE_FROM'], $arHistory['DATE_TO']);
class MyCallback {
private $key;
function __construct($key) {
$this->key = $key;
}
public function callback($matches) {
return sprintf('%s-%s', reset($matches), $this->key);
}
}
$output = 'abca';
$pattern = '/a/';
$key = 'key';
$callback = new MyCallback($key);
$output = preg_replace_callback($pattern, array($callback, 'callback'), $output);
print $output; //prints: a-keybca-key
$output = 'abca';
$pattern = '/a/';
$key = 'key';
$output = preg_replace_callback($pattern, function ($matches) use($key) {
return sprintf('%s-%s', reset($matches), $key);
}, $output);
print $output; //prints: a-keybca-key
function url_get_contents ($Url) {
if (!function_exists('curl_init')){
die('CURL is not installed!');
}
$ch = curl_init();
curl_setopt($ch, CURLOPT_FAILONERROR, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); // allow redirects
curl_setopt($ch, CURLOPT_TIMEOUT, 20); // times out after 4s
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); // return into a variable
curl_setopt($ch, CURLOPT_URL, $Url);
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; ru; rv:1.9.1.5) Gecko/20091102 Firefox/3.5.5 GTB6");
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$output = curl_exec($ch);
curl_close($ch);
return $output;
}
$params = array( 'key' => 'ключ', 'text' => 'Test Message', 'lang' => 'en-ru',);
$query = http_build_query($params);
$response = url_get_contents('https://translate.yandex.net/api/v1.5/tr.json/translate?'.$query);
echo $response;
На чём быстрее
На чем лучше