Difference between revisions of "How to use JDate"

From Joomla! Documentation

m (Reverted edits by Winnersoft1220 (Talk) to last version by Masterchief)
Line 1: Line 1:
 
[[Category:Development]]
 
[[Category:Development]]
 +
== Introduction ==
 +
JDate is a helper class, extended from PHP's DateTime class, which allows developers to handle date formatting more efficiently. The class allows developers to format dates for readable strings, MySQL interaction, UNIX timestamp calculation, and also provides helper methods for working in different timezones.
 +
 +
== 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:
 +
 +
<source lang="php">
 +
$date = new JDate(); // Creates a new JDate object equal to the current time.
 +
</source>
 +
 +
You may also create an instance using the static method defined in JDate:
 +
 +
<source lang="php">
 +
$date = JDate::getInstance(); // Alias of 'new JDate();'
 +
</source>
 +
 +
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:
 +
<source lang="php">$date = JFactory::getDate();</source>
 +
 +
=== 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:
 +
<source lang="php">
 +
$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
 +
</source>
  
 
== Temporary example ==
 
== Temporary example ==
 +
 
JFactory::getDate() gets a JDate object and we then do the JDate toFormat function
 
JFactory::getDate() gets a JDate object and we then do the JDate toFormat function
 
<source lang="php">JFactory::getDate()->toFormat('%a %d %b %Y - %H:%M')</source>
 
<source lang="php">JFactory::getDate()->toFormat('%a %d %b %Y - %H:%M')</source>

Revision as of 08:02, 20 February 2014

Introduction[edit]

JDate is a helper class, extended from PHP's DateTime class, which allows developers to handle date formatting more efficiently. The class allows developers to format dates for readable strings, MySQL interaction, UNIX timestamp calculation, and also provides helper methods for working in different timezones.

Using JDate[edit]

Creating a JDate Instance[edit]

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[edit]

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[edit]

JFactory::getDate() gets a JDate object and we then do the JDate toFormat function

JFactory::getDate()->toFormat('%a %d %b %Y - %H:%M')