Skip to content

Instantly share code, notes, and snippets.

@cristianobecker
cristianobecker / timezone.php
Last active March 7, 2016 14:59
Convert UTC date to a custom timezone in PHP
<?php
function convert_to_timezone($date, $timezone, $format = 'Y-m-d H:i:s') {
$object = date_create_from_format($format, $date, timezone_open('UTC'));
date_timezone_set($object, timezone_open($timezone));
return $object;
}
function convert_to_timezone_string($date, $timezone, $format = 'Y-m-d H:i:s') {
@cristianobecker
cristianobecker / search-files.sh
Last active December 30, 2016 04:24
Fast way to search in files in small projects
search-files() {
local folder=$(test $# -eq 2 && echo "$1" || echo .)
local query=$(test $# -eq 2 && echo "$2" || echo "$1")
local IFS=$'\n'
for f in $(find "$folder" -type f); do
local result=$(
cat "$f" |
GREP_COLOR='01;32' egrep --color=always -n "$query" |
GREP_COLOR='01;33' egrep --color=always "^\d+:"
@cristianobecker
cristianobecker / git-prompt.sh
Last active February 7, 2024 02:46
Git PS1 with current branch and color highligth
RED=$(tput setaf 1)
GREEN=$(tput setaf 2)
YELLOW=$(tput setaf 3)
WHITE=$(tput setaf 7)
NORMAL=$(tput sgr0)
_git-branch() {
local branch="$(git symbolic-ref HEAD 2> /dev/null | sed -e 's/refs\/heads\///')"
test "x$branch" != x && echo " ($branch)"
}
@cristianobecker
cristianobecker / jslint-watch.sh
Last active January 10, 2020 05:30
JSLint watch without gulp or grunt
# Ubuntu: apt-get intall entr
jslint-watch() {
local folder=$(test "x$1" != x && echo "$1" || echo .)
local files="find $folder -type f -name '*.js'"
local modified='$('"$files"' -printf "%T+ %p\n" | sort -r | head -1 | cut -d" " -f2)'
eval $files | entr sh -c 'jslint '"$modified"' --color'
}
# MacOS: brew install entr
@cristianobecker
cristianobecker / main.js
Created June 15, 2015 00:42
Main.js file when using jQuery
/*global jQuery */
/*jslint browser: true */
(function (win, $) {
'use strict';
// helpers
var Util, Globals;
@cristianobecker
cristianobecker / radio.sh
Last active January 14, 2016 16:00
Internet radio to work
radio() {
local radio
local station
local playlist
if test $# -eq 2; then
radio="$1"
station="$2"
elif test $# -eq 0; then
radio=soma
@cristianobecker
cristianobecker / my-proxy.sh
Created May 2, 2015 18:12
Use SSH Tunnel as a SOCKS proxy on MacOS
my-proxy() {
sudo networksetup -setsocksfirewallproxystate Wi-Fi on
sudo networksetup -setsocksfirewallproxy Wi-Fi localhost 8001
ssh cbecker -C -N -D 8001 # change your ssh info here
sudo networksetup -setsocksfirewallproxystate Wi-Fi off
}
@cristianobecker
cristianobecker / convert-xbox.sh
Last active January 14, 2016 16:00
Convert a MKV file to MP4 with subtitles (xbox360 compatible) using ffmpeg
xbox-convert() {
if test $# -eq 1; then
local extension=$(echo "$1" | egrep -o '\.[^\.]+$')
local file=$(echo "$1" | sed -e "s/"$extension"$//")
if test -f "$file.srt" && test -f "$1"; then
ffmpeg -sub_charenc "ISO-8859-1" -i "$file.srt" "$file.ass"
ffmpeg -i "$1" -vf ass="$file.ass" -vcodec libx264 -acodec ac3 -ab 160k "$file.mp4"
rm "$file.ass"
else
echo ".str file and video file should have the same name"
@cristianobecker
cristianobecker / thumb.sh
Created March 6, 2015 17:03
Edit multiple images with imagemagik (resize, optimize and thumbnail)
# RESIZE AND OPTIMIZE (based in height)
HEIGHT=500
mkdir -p result
for f in *.jpg; do
convert "$f" -resize "x${HEIGHT}" -strip -quality 90% "result/$f"
done
# THUMB
WIDTH=200
HEIGHT=225
@cristianobecker
cristianobecker / type.js
Created October 17, 2014 19:24
Get the type of any object in JavaScript
function type(e) {
var t = Object.prototype.toString.call(e),
r = /\[object (\w+)\]/i.exec(t);
return r && r[1].toLowerCase();
}
[1,"2", true, null, [], {}, function() {}, undefined].forEach(function(e){
console.log(type(e))
});