Skip to content

Instantly share code, notes, and snippets.

@jgdoncel
jgdoncel / cast.sql
Created July 30, 2014 12:08
Casteo de campo para ordenar: tenemos un campo varchar, filtramos sólo los registros con valores numéricos y ordenamos con casteo
SELECT id, nombre, apellidos, SUBSTR(CONCAT('0000',codigo),-4) as CDB FROM empleados WHERE codigo REGEXP '^-?[0-9]+$' ORDER BY CAST(codigo AS UNSIGNED) ASC
function deleteDirectoryRecursive($dirPath) {
foreach(new RecursiveIteratorIterator(new RecursiveDirectoryIterator($dirPath, FilesystemIterator::SKIP_DOTS), RecursiveIteratorIterator::CHILD_FIRST) as $path) {
$path->isDir() ? rmdir($path->getPathname()) : unlink($path->getPathname());
}
rmdir($dirPath);
}
@jgdoncel
jgdoncel / bom.php
Created May 30, 2014 16:11
Detectar y eliminar el BOM de un fichero de texto
// Comprobar si el fichero trae BOM
$bom = pack("CCC", 0xef, 0xbb, 0xbf);
if (0 == strncmp($json, $bom, 3)) {
$json = substr($json, 3);
}
@jgdoncel
jgdoncel / fechas.php
Created January 31, 2014 11:44
Manejo de fechas
// Ultimo día del mes actual
$ultimoDia = date('Y/m/t').' 23:59:59';
echo $ultimoDia;
// Sumar segundos a una fecha
$time = strtotime('2013-09-18 11:00:00+25200 seconds');
echo date('H:i',$time);
@jgdoncel
jgdoncel / regExp.php
Created January 30, 2014 08:41
Expresiones regulares
// Eliminar caracterse no alfanuméricos de una cadena
preg_replace("/[^A-Za-z0-9]/", '', $string);
@jgdoncel
jgdoncel / agujaBuffonPi.java
Created January 29, 2014 22:23
Estimar el valor de Pi usando el método de la "Aguja de Buffon" http://http://es.wikipedia.org/wiki/Aguja_de_Buffon
// Tomar una aguja de 1cm y un papel con líneas paralelas a 2cm
// Lanzar la aguja sobre el papel, cada vez que la aguja corta una linea
// cuenta como un acierto
// pi (aprox) = intentos / aciertos
import java.util.Random;
import java.util.Scanner;
public class BuffonPiEstimation
{
$path = $_FILES['image']['name'];
$ext = pathinfo($path, PATHINFO_EXTENSION);
@jgdoncel
jgdoncel / date_add.sql
Created January 16, 2014 10:01
Filtrar en base a fechas utilizando intervalos desde la fecha actual. Calcular una diferencia entre fechas
// Obtener registros de la tabla con fecha superior a hace tres meses
SELECT * FROM tabla WHERE fecha >= DATE_ADD(NOW(),INTERVAL -3 MONTH)
// De otro modo:
SELECT * FROM tabla WHERE fecha >= (NOW() + INTERVAL -3 MONTH)
<?php
// Obtener el array
$myarray = glob("*.*");
// Ordenar por fecha ascendente
usort($myarray, create_function('$a,$b', 'return filemtime($a) - filemtime($b);'));
@jgdoncel
jgdoncel / new_gist_file.sql
Created December 17, 2013 08:24
Transact SQL: Equivalente a GROUP_CONCAT de MySQL
SELECT
STUFF(
(
SELECT
',' + RTRIM(LTRIM(TELEFONO))
FROM CLIENTE
WHERE CLIENTE = '10655'
ORDER BY TELEFONO FOR xml path('')
)
, 1, 1, '')