Skip to content

Instantly share code, notes, and snippets.

View stevenwoodson's full-sized avatar

Steve Woodson stevenwoodson

View GitHub Profile
@stevenwoodson
stevenwoodson / gist:4080143
Created November 15, 2012 18:01
Find Age from Month/Day/Year
function displayage( yr, mon, day, unit, decimal, round ) {
//Sample usage
//displayage (year, month, day, unit, decimals, rounding)
//Unit can be "years", "months", or "days"
//Decimals specifies demical places to round to (ie: 2)
//Rounding can be "roundup" or "rounddown"
//displayage(1997, 11, 24, "years", 0, "rounddown")
today = new Date();
yr = parseInt(yr, 10);
@stevenwoodson
stevenwoodson / MY_DB_mysql_driver.php
Created December 27, 2012 06:10
Codeigniter DB - Insert on Duplicate Update method Generates a platform-specific insert string from the supplied data, MODIFIED to include ON DUPLICATE UPDATE
<?php if (! defined('BASEPATH')) exit('No direct script access allowed');
class MY_DB_mysql_driver extends CI_DB_mysql_driver {
final public function __construct($params) {
parent::__construct($params);
}
/**
* Insert_On_Duplicate_Update_Batch
@stevenwoodson
stevenwoodson / after-binding.js
Created December 29, 2012 21:05
jQuery - Custom binding on 'after'
// Trigger any custom bindings on after
(function($) {
var origAfter = $.fn.after;
$.fn.after = function () {
return origAfter.apply(this, arguments).trigger("after");
};
})(jQuery);
// Bind 'after' and then use it to trigger function_to_trigger()
$('.element').bind("after", function() { function_to_trigger(); } }).after('<ul></ul>');
@stevenwoodson
stevenwoodson / gist:4465615
Last active January 19, 2017 00:56
Set the local users timezone offset to a cookie with Javascript and pull it with PHP to set the default timezone
/**
* Set the local time offset to display time values in the users local time
*/
function setTimeOffset() {
// Create all the dates necessary
var now = later = d1 = d2 = new Date(),
set = { 'offset': now.getTimezoneOffset(), 'dst': 0 }
// Set time for how long the cookie should be saved - 1 year
later.setTime(now.getTime() + 365 * 24 * 60 * 60 * 1000);
// Date one is set to January 1st of this year
@stevenwoodson
stevenwoodson / KeyTerms.php
Created March 16, 2013 21:35
Determines the key terms in a provided $content string and returns the $return most popular terms. This works by clearing all the most common words from the content and counting the number of occurrences of what's left. We then find all phrases that contain any of those words that occurred more than once in an attempt to isolate the most importa…
<?php
/**
* Key Terms
*
* Determines the key terms in a provided $content string and returns the $return most popular terms. This works by
* clearing all the most common words from the content and counting the number of occurrences of what's left. We then
* find all phrases that contain any of those words that occurred more than once in an attempt to isolate the most
* important phrases. The number of terms returned can also be set or set to 0 to return all matches. If there's room
* for more returned results ($results has to be greater than zero) then we gather the most used words not in an
* existing phrase and fill the returned array.
@stevenwoodson
stevenwoodson / ipMatch.php
Created January 9, 2014 05:51
Takes an $ipAddress as a string and an array of IP Addresses and/or IP wildcards $toMatch it with, returns boolean true if a match is found and false otherwise
<?php
/**
* IP Address Match
*
* Takes an $ipAddress as a string and an array of IP Addresses and/or IP wildcards $toMatch it with, returns boolean
* true if a match is found and false otherwise
*
* @param string $ipAddress
* @param array $toMatch
@stevenwoodson
stevenwoodson / start.php
Last active August 29, 2015 13:56
Laravel 4 Detect environment set to use the servers SERVER_NAME, default to local for artisan commands
$env = $app->detectEnvironment(function(){
// For artisan commands, default to localhost
$serverName = 'localhost';
if ( isset($_SERVER["SERVER_NAME"]) && strlen($_SERVER["SERVER_NAME"]) > 0 ){
$serverName = $_SERVER["SERVER_NAME"];
}
// List of environments with the server name as the key
$serverNames = array(
'staging.net' => 'staging',
'productionserver.com' => 'production'
@stevenwoodson
stevenwoodson / gist:9534480
Created March 13, 2014 18:52
Laravel 4 - dump most recent queries
dd(DB::getQueryLog());
@stevenwoodson
stevenwoodson / gist:9578083
Created March 16, 2014 03:19
Laravel 4 Custom Validation Rule - Date not today
// Return an error if $value day is today
Validator::extend('before_today', function($attribute, $value, $parameters) {
if ( strtotime( $value ) >= strtotime( date("Y-m-d") ) ) {
return false;
}
return true;
});
@stevenwoodson
stevenwoodson / gist:9675310
Created March 20, 2014 22:26
Laravel 4 Custom Validation - check for field already used today
// Return an error if $value was found in a record from today
Validator::extend('unique_today', function($attribute, $value, $parameters) {
if ( $this->findAll( null, array( $attribute => $value, 'from' => date('Y-m-d') ) )->count() ) {
return false;
}
return true;
});
Validator::replacer('unique_today', function($message, $attribute, $rule, $parameters) {
return Lang::get('error_enteredtoday', array('attribute' => $attribute));
});