Difference between revisions of "How to use JDate/es"
From Joomla! Documentation
(Created page with "Cómo utilizar JDate") |
(Created page with "== Introducción == JDate es una clase auxiliar, se extiende de la clase DateTime de PHP, permite a los desarrolladores manejar el formato de fechas de forma más eficiente....") |
||
Line 1: | Line 1: | ||
<noinclude><languages /></noinclude> | <noinclude><languages /></noinclude> | ||
− | == | + | == Introducción == |
− | JDate | + | JDate es una clase auxiliar, se extiende de la clase DateTime de PHP, permite a los desarrolladores manejar el formato de fechas de forma más eficiente. La clase permite a los desarrolladores dar formato legibles a cadenas fechas, interacción MySQL, cálculo timestamp de UNIX y también proporciona métodos auxiliares para trabajar con diferentes zonas horarias. |
== Using JDate == | == Using JDate == |
Revision as of 16:19, 29 October 2015
Introducción
JDate es una clase auxiliar, se extiende de la clase DateTime de PHP, permite a los desarrolladores manejar el formato de fechas de forma más eficiente. La clase permite a los desarrolladores dar formato legibles a cadenas fechas, interacción MySQL, cálculo timestamp de UNIX y también proporciona métodos auxiliares para trabajar con diferentes zonas horarias.
Using JDate
Creating a JDate Instance
All of the date helper methods require an instance of the JDate class. To begin, you must create one. A JDate object may be created in two ways. One is the typical native method of simply creating a new instance:
$date = new JDate(); // Creates a new JDate object equal to the current time.
You may also create an instance using the static method defined in JDate:
$date = JDate::getInstance(); // Alias of 'new JDate();'
There is no difference between these methods, as JDate::getInstance simply creates a new instance of JDate exactly like the first method shown.
Alternatively, you may also retrieve the current date (as a JDate object) from JApplication, by using:
$date = JFactory::getDate();
Arguments
The JDate constructor (and getInstance static method) accepts two optional parameters: A date string to format, and a timezone. Not passing a date string will create a JDate object with the current date and time, while not passing a timezone will allow the JDate object to use the default timezone set.
The first argument, if used, must be a string that can be parsed using php's native DateTime constructor. E.g:
$currentTime = new JDate('now'); // Current date and time
$tomorrowTime = new JDate('now +1 day'); // Current date and time, + 1 day.
$date = new JDate('2012-12-1 15:20:00'); // 3:20 PM, December 1st, 2012
Temporary example
JFactory::getDate() gets a JDate object and we then do the JDate toFormat function
JFactory::getDate()->toFormat('%a %d %b %Y - %H:%M')
In Joomla! CMS 3.3 API the function "toFormat" was changed to "format", so the example above should be:
JFactory::getDate()->format('%a %d %b %Y - %H:%M')