Skip to content

Instantly share code, notes, and snippets.

@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 / 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 / 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);
}
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 / 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
@jgdoncel
jgdoncel / custom_order.sql
Created December 11, 2014 09:59
Orden personalizado en MySQL
SELECT * FROM tabla ORDER BY FIELD(tipo,'ANTES','DURANTE','DESPUES') ASC
@jgdoncel
jgdoncel / partition_by_month.sql
Last active August 29, 2015 14:22
Particionar tabla Ejemplo suponiendo una tabla test (id,fecha) que pretendemos particionar por meses. Conviene crear el campo para el HASH debido a que MySQL filtra las consultas por particiones siempre que se incluya el HASH en el filtro. https://dev.mysql.com/doc/refman/5.6/en/partitioning-pruning.html
-- Crear nuevo campo
ALTER TABLE `test` ADD COLUMN `ts_year_month` int(11);
-- Insertar valores en el campo para los registros actuales
UPDATE `test` SET ts_year_month = DATE_FORMAT(fecha,'%Y%m');
-- Actualizar la definición de la nueva columna
ALTER TABLE `test` MODIFY `ts_year_month` int(11) NOT NULL;
-- Añadir el nuevo campo al índice
@jgdoncel
jgdoncel / bootstrap-modal.html
Last active November 30, 2016 11:14
Boostrap modal fullscreen
<div class="modal fade" id="modal-id" tabindex="-1" role="dialog">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
<h4>Título</h4>
</div>
<div class="modal-body" style="max-height: calc(100vh - 212px);overflow-y: auto;">
<div class="list-group">
</div>
@jgdoncel
jgdoncel / single_redirect.php
Created March 29, 2016 19:04
Redirección de página con htaccess
RewriteRule ^loquesea$ http://www.otrodominio.com/otrodirectorio [R=301,NC,L]
@jgdoncel
jgdoncel / new_gist_file_0
Created April 12, 2016 12:59
MYSQL SET UTC
mysql> SELECT CURRENT_TIMESTAMP();
+---------------------+
| CURRENT_TIMESTAMP() |
+---------------------+
| 2016-04-12 14:31:59 |
+---------------------+
1 row in set
mysql> SELECT @@global.time_zone, @@session.time_zone;
+--------------------+---------------------+