Skip to content

Instantly share code, notes, and snippets.

View Foxy79's full-sized avatar

Aleksey Borisov Foxy79

View GitHub Profile
<?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; }
@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 hoursDotMinutes($seconds) {
$h = floor($seconds/3600);
$seconds -= $h*3600;
$m = floor($seconds/60);
return sprintf("%02d:%02d",$h,$m);
}
<?php
function num2chars($n) {
$cs = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
$s = strlen($cs);
$str = "";
while($n) {
$str = $cs{$n%$s}.$str;
$n = floor($n/$s);
}
<?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
class db {
public $isConnected;
protected $datab;
public function __construct($username, $password, $host, $dbname, $options=array()){
$this->isConnected = true;
try {
$this->datab = new PDO("mysql:host={$host};dbname={$dbname};charset=utf8", $username, $password, $options);
$this->datab->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
@Foxy79
Foxy79 / obj2arr.php
Last active February 24, 2016 14:26
<?php
function object_to_array($data) {
if (is_array($data) || is_object($data)) {
$result = array();
foreach ($data as $key => $value) { $result[$key] = object_to_array($value); }
return $result;
}
return $data;
}
<?php
function ev_mb_ucfirst($str) {
$fc = mb_strtoupper(mb_substr($str, 0, 1));
return $fc.mb_substr($str, 1);
}
<?php
if (!function_exists('getallheaders')) {
function getallheaders() {
$headers = '';
foreach ($_SERVER as $name => $value) {
if (substr($name, 0, 5) == 'HTTP_') {
$headers[str_replace(' ', '-', ucwords(strtolower(str_replace('_', ' ', substr($name, 5)))))] = $value;
}
}
@Foxy79
Foxy79 / geo.sql
Created February 26, 2016 08:56
Mysql sample query finding points with distanse from current coords 500m
-- 55.88846600 = current latitude
-- 37.44916300 = current longitude
SELECT Title, ROUND( 6371000 * acos( cos( radians('55.88846600') ) * cos( radians( Lat ) ) * cos( radians( Lng ) - radians('37.44916300') ) + sin( radians('55.88846600') ) * sin( radians( Lat ) ) ) ) AS distance FROM Points HAVING distance < 500 OR distance IS NULL ORDER BY distance LIMIT 1