Source for file date_functions.php
Documentation is available at date_functions.php
* An open source application development framework for PHP 4.3.2 or newer
* @author ExpressionEngine Dev Team
* @copyright Copyright (c) 2006, EllisLab, Inc.
* @license http://codeigniter.com/user_guide/license.html
* @link http://codeigniter.com
// ------------------------------------------------------------------------
* CodeIgniter Date Helpers
* @author ExpressionEngine Dev Team
* @link http://codeigniter.com/user_guide/helpers/date_helper.html
// ------------------------------------------------------------------------
* Convert MySQL Style Datecodes
* This function is identical to PHPs date() function,
* except that it allows date codes to be formatted using
* the MySQL style, where each code letter is preceded
* with a percent sign: %Y %m %d etc...
* The benefit of doing dates this way is that you don't
* have to worry about escaping your text letters that
function mdate($datestr, $time =
false) {
return date($datestr, $time);
// ------------------------------------------------------------------------
* Number of days in a month
* Takes a month/year as input and returns the number of days
* for the given month/year. Takes leap years into consideration.
* @param integer a numeric month
* @param integer a numeric year
if ($month <
1 OR $month >
12) {
if ($year %
400 ==
0 ||
($year %
4 ==
0 AND $year %
100 !=
0)) {
$days_in_month =
array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
return $days_in_month[$month -
1];
// ------------------------------------------------------------------------
* Converts a local Unix timestamp to GMT
* @param integer Unix timestamp
return mktime( gmdate("H", $time), gmdate("i", $time), gmdate("s", $time), gmdate("m", $time), gmdate("d", $time), gmdate("Y", $time));
// ------------------------------------------------------------------------
* Converts GMT time to a localized value
* Takes a Unix timestamp (in GMT) as input, and returns
* at the local value based on the timezone and DST setting
* @param integer Unix timestamp
* @param bool whether DST is active
function gmt_to_local($time =
false, $timezone =
'UTC', $dst =
FALSE) {
// ------------------------------------------------------------------------
* Converts a MySQL Timestamp to Unix
* @param integer Unix timestamp
// We'll remove certain characters for backward compatibility
// since the formatting changed with MySQL 4.1
// ------------------------------------------------------------------------
* Convierte una fecha en formato Unix a MySql
* @param integer Unix timestamp
* @param bool whether to show seconds
return date('Y-m-d H:i:s', $time);
return date('Y-m-d', $time);
// ------------------------------------------------------------------------
* Returns an array of timezones. This is a helper function
* for various other ones in this library
// Note: Don't change the order of these even though
// some items appear to be in the wrong order
return ( ! isset
($zones[$tz])) ?
false :
$zones[$tz];
* Deuvelve la fecha en formato <i>$cFormat</i>.
* %A = Lunes, Martes, etc
* %B = Enero, Febrero, etc
function spanish_date($cFormat=
'%A, %e de %B de %Y', $nTimestamp=
null, $cLocale =
APP_LOCALE) {
// Establece informacion de localidad.
// Si no se paso la marca de tiempo pone una por defecto.
if ($nTimestamp ===
null) {
// Mapeo para argumentos NO-WINDOWS
'%r' =>
date("h:i:s", $nTimestamp) .
" %p",
'%R' =>
date("H:i", $nTimestamp),
'%u' =>
($w =
date("w", $nTimestamp)) ?
$w :
7
$cFecha =
strftime($cFormat, $nTimestamp);
* Deuvelve la fecha para MySql.
* El parametro $cFecha recibe YYYY/DD/MM (formato ingles)
return "{
$aFecha[0]}-{$aFecha[2]}-{$aFecha[1]}";
* Deuvelve la fecha en formato ingles.
* El parametro $cFecha recibe YYYY-MM-DD (mysql)
return "{
$aFecha[0]}/{$aFecha[2]}/{$aFecha[1]}";
* Formatea una hora para poder usarla en MySql.
* El parametro $cHora puede ser algo como esto:
* El parametro $nForce es para forzar la hora a cero
* en caso que $cHora corresponda a un horario incorrecto, EJ:
if (@$aHora[0] <
0 ||
@$aHora[0] >
23) {
if (@$aHora[1] <
0 ||
@$aHora[1] >
59) {
if (@$aHora[2] <
0 ||
@$aHora[2] >
59) {
$cReturn =
sprintf('%02d', @$aHora[0]).
* Recibe una fecha o fecha/hora en formato de MySql (YYYY-MM-DD HH:MM:SS) y la
* pasa al formato especificado por $cFormat.
* Para saber mas sobre el formato consultar la documentacion de la funcion strftime
* @param string $cMySqlDate
function mysql2date($cMySqlDate, $cFormat=
'%d/%m/%Y') {
if (!preg_match("/([0-9]{4})-([0-9]{2})-([0-9]{2}) ([0-9]{2}):([0-9]{2}):([0-9]{2})/", $cMySqlDate, $aMySqlDate)) {
preg_match("/([0-9]{4})-([0-9]{2})-([0-9]{2})/", $cMySqlDate, $aMySqlDate);
$cReturn =
str_replace("%d", $aMySqlDate[3], $cReturn);
$cReturn =
str_replace("%m", $aMySqlDate[2], $cReturn);
$cReturn =
str_replace("%Y", $aMySqlDate[1], $cReturn);
if (isset
($aMySqlDate[4])) {
$cReturn =
str_replace("%H", $aMySqlDate[4], $cReturn);
$cReturn =
str_replace("%i", @$aMySqlDate[5], $cReturn);
$cReturn =
str_replace("%s", @$aMySqlDate[6], $cReturn);
return date($cDateStr, $nMarca);
* Recibe una fecha o fecha/hora en formato de MySql (YYYY-MM-DD HH:MM:SS) y la
* pasa al formato especificado por $cFormat.
* Para saber mas sobre el formato consultar la documentacion de la funcion strftime
* @param string $cMySqlDate
function date2mysql($cDate, $cFormat=
'%d/%m/%Y', $nTime=
true) {
if ($cDate ==
'' ||
$cDate ==
'__/__/____') {
// Se definen los reemplazos por formato (el formato es igual al que usa strftime).
$aReemplazos['%d'] =
array('f' =>
'([0-9]{1,2})', 'id' =>
2);
$aReemplazos['%m'] =
array('f' =>
'([0-9]{1,2})', 'id' =>
1);
$aReemplazos['%y'] =
array('f' =>
'([0-9]{2})', 'id' =>
0);
$aReemplazos['%Y'] =
array('f' =>
'([0-9]{2,4})', 'id' =>
0);
$aReemplazos['%H'] =
array('f' =>
'([0-9]{2})', 'id' =>
3);
$aReemplazos['%i'] =
array('f' =>
'([0-9]{2})', 'id' =>
4);
$aReemplazos['%s'] =
array('f' =>
'([0-9]{2})', 'id' =>
5);
$aReemplazosVacios['%d'] =
'[0-9]{1,2}';
$aReemplazosVacios['%m'] =
'[0-9]{1,2}';
$aReemplazosVacios['%y'] =
'[0-9]{2}';
$aReemplazosVacios['%Y'] =
'[0-9]{2,4}';
$aReemplazosVacios['%H'] =
'[0-9]{2}';
$aReemplazosVacios['%i'] =
'[0-9]{2}';
$aReemplazosVacios['%s'] =
'[0-9]{2}';
$aMySqlDate =
array('0000', '00', '00', '00', '00', '00'); // Define los datos de fecha para MySql.
// Cicla por todos los formatos de reemplazo.
foreach ($aReemplazos as $cClave =>
$aValores) {
// Prepara la expresion regular.
$cFormatoTmp =
str_replace($cClave, $aValores['f'], $cFormat);
$cFormatoTmp =
strtr($cFormatoTmp, $aReemplazosVacios);
// Realiza la busqueda del dato de fecha, si la encuentra la pone en el array que contiene
// los datos de fecha para mysql.
if (preg_match("/{$cFormatoTmp}/", $cDate, $aFechaTmp)) {
if (count($aFechaTmp) ==
2) {
$aMySqlDate[$aValores['id']] =
$aFechaTmp[1];
if (strlen($aMySqlDate[0]) ==
2) {
$aMySqlDate[0] =
"20".
$aMySqlDate[0];
// Genera la fecha para MySql en el formato adecuado.
$cReturn =
"{
$aMySqlDate[0]}-{
$aMySqlDate[1]}-{
$aMySqlDate[2]} {
$aMySqlDate[3]}:{
$aMySqlDate[4]}:{
$aMySqlDate[5]}";
$cReturn =
"{
$aMySqlDate[0]}-{
$aMySqlDate[1]}-{
$aMySqlDate[2]}";
if ($cReturn ==
'0000-00-00 00:00:00' ||
$cReturn ==
'0000-00-00') {
* Devuelve la diferencia entre dos fechas.
$nDiff = (int)
(($nFecha1 -
$nFecha2) /
86400);
* Convierte una cantidad decimal a horas y minutos: 3.25 -> 03:15, 2.50 -> 02:30.
* @param string $nDecimal
* Convierte una cantidad de segundos a formato HH:ii
* @param string $nSegundos
Documentation generated on Tue, 22 Nov 2011 13:28:53 -0200 by phpDocumentor 1.4.3