Skip to content

Instantly share code, notes, and snippets.

View calvinchoy's full-sized avatar

Calvin Choy calvinchoy

View GitHub Profile
@calvinchoy
calvinchoy / PHP create and modify date.php
Last active August 4, 2020 13:18
PHP create and modify date
<?php
//create and modifying dates month
//creating new current date
$date_from = date('Y-m-01'); //first day of the current month
$date_to = date('Y-m-t'); //last day of the current month using t, use d for
//creating previous month date
$date_from_prev = date('Y-m-01', strtotime("first day of last month"));
$date_to_prev = date('Y-m-t', strtotime("last day of last month"));
@calvinchoy
calvinchoy / PHP - get first key and value from array.php
Created March 19, 2013 12:42
PHP get first key and value from array
<?php
//Get first key and value from an array
$first_value = reset($array);
$first_key = key($array);
?>
@calvinchoy
calvinchoy / JS - simple ajax call.js
Last active April 4, 2020 09:00
JS - simple ajax call #jquery
$.ajax({
type: "POST",
url: "url to the server processing code",
dataType: "json",
contentType: "application/json",
data: JSON.stringify({}),
success: function(response) {
//response is json data returned from server side url
}
});
@calvinchoy
calvinchoy / JS - ajax global callbacks.js
Last active April 4, 2020 09:00
JS - ajax global callbacks #jquery
//on start
$('body').ajaxStart(function () {
$('#ajax-status').show().text("Loading...");
});
//on stop
$('body').ajaxStop(function () {
$('#ajax-status').fadeOut();
});
//on error
$('body').ajaxError(function (event, xhr, ajaxOptions, thrownError) {
@calvinchoy
calvinchoy / SH - ping monitoring.sh
Created April 5, 2013 08:53
SH - ping monitoring
#!/bin/bash
# Simple SHELL script for Linux and UNIX system monitoring with
# ping command
# -------------------------------------------------------------------------
# Copyright (c) 2006 nixCraft project <http://www.cyberciti.biz/fb/>
# This script is licensed under GNU GPL version 2.0 or above
# -------------------------------------------------------------------------
# This script is part of nixCraft shell script collection (NSSC)
# Visit http://bash.cyberciti.biz/ for more information.
# -------------------------------------------------------------------------
@calvinchoy
calvinchoy / PHP - date with ms.php
Created April 16, 2013 13:02
PHP - Date with with ms
<?php
function udate($format, $utimestamp = null) {
if (is_null($utimestamp))
$utimestamp = microtime(true);
$timestamp = floor($utimestamp);
$milliseconds = round(($utimestamp - $timestamp) * 1000000);
return date(preg_replace('`(?<!\\\\)u`', $milliseconds, $format), $timestamp);
}
@calvinchoy
calvinchoy / JS - Date browser fix.js
Last active December 18, 2015 14:19
JS - Date browser fix
//Date fix with some old browsers
if (!Date.now) {
Date.now = function() {
return new Date().valueOf();
}
}
@calvinchoy
calvinchoy / JS - Child duplicator.js
Last active April 4, 2020 09:00
JS - Child duplicator #jquery
$(".btn-add-child").click(function() {
var newItem;
newItem = $("body .child.item.duplicate").clone().removeClass("duplicate");
newItem.appendTo(".parent");
$(".parent .child.item").show();
return $(".btn-remove-child").click(function() {
return $(this).parent().remove();
});
});
@calvinchoy
calvinchoy / PHP - udate microformat.php
Created June 20, 2013 08:37
PHP - udate, transform date in micro format
<?php
function udate($format, $utimestamp = null) {
if (is_null($utimestamp))
$utimestamp = microtime(true);
$timestamp = floor($utimestamp);
$milliseconds = round(($utimestamp - $timestamp) * 1000000);
return date(preg_replace('`(?<!\\\\)u`', $milliseconds, $format), $timestamp);
}
@calvinchoy
calvinchoy / PHP - ip and bigint converter.php
Created June 20, 2013 08:38
PHP - ip and bigint converter
<?php
//Helper function to convert bigint to ip adress
function int2ip($intip){
$ipVal = $intip;
$ipArr = array(0 => floor($ipVal/0x1000000) );
$ipVint = $ipVal-($ipArr[0]*0x1000000); // for clarity
$ipArr[1] = ($ipVint & 0xFF0000) >> 16;
$ipArr[2] = ($ipVint & 0xFF00 ) >> 8;
$ipArr[3] = $ipVint & 0xFF;
$ipDotted = implode('.', $ipArr);