Skip to content

Instantly share code, notes, and snippets.

View luckyshot's full-sized avatar
🌍
xaviesteve.com

Xavi Esteve luckyshot

🌍
xaviesteve.com
View GitHub Profile
@luckyshot
luckyshot / dates.php
Last active June 30, 2016 14:57
PHP Dates and times examples
<?php
// Full documentation:
// http://php.net/manual/en/function.date.php
// Set timezone to avoid PHP warning
date_default_timezone_set( 'Europe/London' );
date_default_timezone_set( 'Europe/Madrid' );
@luckyshot
luckyshot / abusecheck.php
Last active April 28, 2023 07:46
Throttle client requests to avoid DoS attack (scroll down for IP-based)
<?php
/**
ABUSE CHECK
Throttle client requests to avoid DoS attack
*/
session_start();
$usage = array(5,5,5,5,10,20,30,40,50,60,120,180,240); // seconds to wait after each request
if (isset($_SESSION['use_last'])) {
$nextin = $_SESSION['use_last']+$usage[$_SESSION['use_count']];
@luckyshot
luckyshot / new_gist_file
Created September 29, 2013 00:00
Bayesian Rating algorythm
The formula for calculating the Top Rated 250 Titles gives a true Bayesian estimate:
weighted rating (WR) = (v ÷ (v+m)) × R + (m ÷ (v+m)) × C
where:
R = average for the movie (mean) = (Rating)
v = number of votes for the movie = (votes)
m = minimum votes required to be listed in the Top 250 (currently 25000)
C = the mean vote across the whole report (currently 7.0)
@luckyshot
luckyshot / new_gist_file.js
Created September 30, 2013 00:01
jQuery AJAX Requests
$.ajax({
type: "POST",
url: "some.php",
data: { name: "John", location: "Boston" }
})
.done(function( data ) {
data = $.parseJSON(data);
console.log(data);
})
.fail(function( jqXHR, textStatus ) {
@luckyshot
luckyshot / new_gist_file.php
Created September 30, 2013 01:20
Nice number abbreviation (i.e. 12.5k)
function abbreviate_number($num) {
if ($num >= pow(10,3) AND $num < pow(10,4)) {
return round($num/1000, 1).'k';
}else if ($num >= pow(10,4) AND $num < pow(10,5)) {
return round($num/1000, 1).'k';
}else if ($num >= pow(10,5) AND $num < pow(10,6)) {
return round($num/1000, 0).'k';
}else if ($num >= pow(10,6) AND $num < pow(10,7)) {
return round($num/1000000, 1).'m';
}else if ($num >= pow(10,7) AND $num < pow(10,8)) {
@luckyshot
luckyshot / timeago.php
Last active May 28, 2019 16:15
Nice relative time (ago)
<?php
protected function nice_time($timestamp) {
if ( !$timestamp ){
return '';
}
// if no timestamp, do it now
if ( !is_numeric($timestamp) ){ $timestamp = strtotime( $timestamp ); }
// $periods = array("second", "minute", "hour", "day", "week", "month", "year", "decade");
$periods = [$this->_("second"), $this->_("minute"), $this->_("hour"), $this->_("day"), $this->_("week"), $this->_("month"), $this->_("year"), $this->_("decade")];
@luckyshot
luckyshot / new_gist_file.php
Created October 1, 2013 19:47
PHP/MySQLi method (with parametrization)
<?php
// Config
define('MYSQL_SERVER', 'localhost');
define('MYSQL_DATABASE', 'database');
define('MYSQL_USER', 'root');
define('MYSQL_PASS', 'pass');
// MySQLi class method
@luckyshot
luckyshot / new_gist_file.php
Created October 3, 2013 00:11
Smart config.php Development / Production server
<?php
define('DEBUG', in_array($_SERVER['HTTP_HOST'], array('127.0.0.1', 'localhost')) );
if (DEBUG==false) {
// PRODUCTION SEVER
error_reporting(0);
define('APP_URL', 'http://www.example.com/');
define('MYSQL_SERVER', 'localhost'); //
@luckyshot
luckyshot / en.php
Created October 9, 2013 23:19
Simple PHP i18n (internationalization) (Haven't tested it, written on the fly)
<?php
$lang = array(
'createaccount' => 'Create Account',
'welcome' => 'Welcome %s!',
// ...
);
@luckyshot
luckyshot / date.js
Created October 31, 2013 14:16
Run only once every X seconds (timer)
/**
* This version uses Date to detect cooldown
* Much better performance than with Timer
*/
var cooldownTime = 2000, // ms
nextRun = null;
function run() {
// Your code here