Skip to content

Instantly share code, notes, and snippets.

View Foxy79's full-sized avatar

Aleksey Borisov Foxy79

View GitHub Profile
<?php
function cidr2mask($mask){
return long2ip(pow(2,32) - pow(2, (32-$mask)));
}
function mask2cidr($mask){
$a=strpos(decbin(ip2long($mask)),"0");
if (!$a){$a=32;}
return $a;
<?php
function num2chars($n) {
$cs = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
$s = strlen($cs);
$str = "";
while($n) {
$str = $cs{$n%$s}.$str;
$n = floor($n/$s);
}
<?php
function hoursDotMinutes($seconds) {
$h = floor($seconds/3600);
$seconds -= $h*3600;
$m = floor($seconds/60);
return sprintf("%02d:%02d",$h,$m);
}
@Foxy79
Foxy79 / cardNumberValidate.php
Last active February 24, 2016 15:44
Luhn algorithm implementation for validate credit card number
<?php
/*
-(bool) luhnValidate ("number");
*/
function luhnValidate($string) {
$string = preg_replace("![^0-9]!","",$string);
$result = "";
foreach (str_split(strrev((string) $string)) as $i => $d) { $result .= $i %2 !== 0 ? $d * 2 : $d; }
<?php
function TimeRangeRusString($time1,$time2) {
$diff = $time2-$time1;
if ($diff==0) return "0 секунд";
$str = array();
if ($diff>=2592000) { $h = floor($diff/2592000); $str[] = omNumber($h, array('месяц','месяца','месяцев')); $diff-=$h*2592000; }
if ($diff>=604800 && count($str)<1) { $h = floor($diff/604800); $str[] = omNumber($h, array('неделю','недели','недель')); $diff-=$h*604800; }
if ($diff>=86400 && count($str)<1) { $h = floor($diff/86400); $str[] = omNumber($h, array('день','дня','дней')); $diff-=$h*86400; }
if ($diff>=3600 && count($str)<1) { $h = floor($diff/3600); $str[] = omNumber($h, array('час','часа','часов')); $diff-=$h*3600; }