Skip to content

Instantly share code, notes, and snippets.

@olenkadark
Created July 8, 2021 10:08
Show Gist options
  • Save olenkadark/557b8fcef61ab8b2cb5b22a3ebc50382 to your computer and use it in GitHub Desktop.
Save olenkadark/557b8fcef61ab8b2cb5b22a3ebc50382 to your computer and use it in GitHub Desktop.
<?php
class DateTasks{
/**
* @param string $period
*
* @return false|string
*/
public static function getPasteDate($period){
return self::getDate('-' . str_replace('-', '', $period));
}
/**
* @param string $period
*
* @return false|string
*/
public static function getDate($period){
return date('Y-m-d H:i:s', strtotime( $period, time() ) );
}
}
class RoundNumber{
/**
* @param int $d
*
* @return int
*/
public static function run($d){
return $d % 50 > 24 ? $d + (50 - $d % 50) : $d - $d % 50;
}
}
class ArrayChangeKeyCaseToUpper{
/**
* @param array $array
*
* @return array
*/
public static function run($array){
$keys = array_keys($array);
$values = array_values($array);
$keys = array_map(function ($v){
return strtoupper($v);
}, $keys);
return array_combine($keys, $values);
}
}
class XMLtoARRAY{
/**
* @param string $xml_string
*
* @return array
*/
public static function run($xml_string){
$xml = simplexml_load_string($xml_string, "SimpleXMLElement", LIBXML_NOCDATA);
$json = json_encode($xml);
return json_decode($json,TRUE);
}
}
echo '<pre>';
echo 'Получить вчерашнюю дату минус один час' . PHP_EOL;
echo DateTasks::getPasteDate('1 hour') . PHP_EOL . PHP_EOL;
echo 'Округлить число до 50, например число 3654 должно быть округлено до 3650, а число 3678 до 3700' . PHP_EOL;
echo '3654 - ' . RoundNumber::run(3654) . PHP_EOL;
echo '3678 - ' . RoundNumber::run(3678) . PHP_EOL;
echo '3675 - ' . RoundNumber::run(3675) . PHP_EOL;
echo '3673 - ' . RoundNumber::run(3673) . PHP_EOL . PHP_EOL;
echo 'Изменить все ключи в массиве на верхний регистр.' . PHP_EOL;
var_dump( ArrayChangeKeyCaseToUpper::run( ['green' => 'avocado', 'red' => 'apple', 'yellow' => 'banana']) );
echo PHP_EOL . PHP_EOL;
echo 'Преобразовать XML в массив.' . PHP_EOL;
$xml_string = "<note>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>";
var_dump( XMLtoARRAY::run( $xml_string ) );
echo PHP_EOL . PHP_EOL;
echo '</pre>';
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment