elxisDate

Last update by Elxis Team

Manage date and time

Elxis sets timezone to UTC (Greenwitch time). All system dates are stored in UTC. The public shown dates can be displayed in a different timezone. More over a user can pick his desired timezone overwriting site default.

Elxis internal date time (UTC): 2010-12-25 08:23:10
Site public date time (Europe/Athens): 2010-12-25 10:23:10
User selected date time (Asia/Tokyo): 2010-12-25 17:23:10

elxisDate class

The elxisDate class formats date and time and also handles conversions from system (Elxis/UTC) to local (site/user) time or the opposite. elxisDate instance can be called via eFactory like this:

$eDate = eFactory::getDate();

 

There are 2 major methods to format dates.

 

formatTS

Format a given unix timestamp to local or system datetime string by using strftime style format arguments.

public function formatTS($ts, $format, $local=true)
Use $ts = 0 for current date, example:
$eDate->formatTS(0, '%A %B %d, %Y %H:%M');

 

formatDate

 

Format a given datetime string to local or system datetime string by using strftime style format arguments. The given datetime should be in this format: YYYY-mm-dd HH:ii:ss

public function formatDate($date, $format, $local=true)
Sample usage:
$eDate->formatDate('2011-05-13 18:34:10', '%A %B %d, %Y %H:%M');
Tip: set $date = '' or 'now' for current date time.

 

World datetime

 

An other cool feature of elxisDate class is that you can display any datetime for any place of the world. Use worldDate method feeded by a datetime string in UTC, a timezone and your desired format.

public function worldDate($dateUTC, $timezone='UTC', $format='%A %d %B %Y %H:%M:%S')

 

$eDate = eFactory::getDate();

 

echo 'Current date in Athens: '.$eDate->worldDate('', 'Europe/Athens');
Current date in Athens: Saturday 25 December 2010 11:12:26
echo 'Current date in Tokyo: '.$eDate->worldDate('', 'Asia/Tokyo');
Current date in Tokyo: Saturday 25 December 2010 18:12:26
echo 'Current date in Melbourne: '.$eDate->worldDate('', 'Australia/Melbourne');
Current date in Melbourne: Saturday 25 December 2010 20:12:26

 

elxisLocalDate interface

 

elxisDate can optionally generate custom datetime strings for specific languages. There is an interface class named elxisLocalDate that can be used to provide custom date formatting. The classes that implements this interface are stored in this folder:
includes/libraries/elxis/date/
following this name pattern: {language}.date.php (el.date.php, fa.date.php, it.date.php, etc)

elxisLocalDate interface has the following methods:

  • __construct used to trigger the implementation class constructor.
  • local_strftime used to provide custom formatting to formatTS method.

 

The classes that implements the elxisLocalDate interface should be named by following this name pattern: elxisDate{language} examples: elxisDateel, elxisDatefa, elxisDateit, etc...

 

class elxisDatefa implements elxisLocalDate {
public function __construct() {}
public function local_strftime($format, $ts) {}
}

 

The need for date interfaces

 

Many languages need custom date formatting. For example fa (Farsi) dates should follow the Jalaly calendar and not the Julian or the Gregorian one. Also fa has different symbols for the numbers.

English datetime (Gregorian): Saturday 25 December 2010 10:55:07
Farsi datetime (Jalaly with latin numbers): يكشنبه 04 دي 1389 10:58:17
Farsi datetime (Jalaly with jalaly numbers): يكشنبه ۰۴ دي ۱۳۸۹ ۱۰:۵۷:۱۹

Note that the year in Jalaly is 1389 and not 2010! The same for months and days.

For el (Greek) months names should be displayed differently when they used on dates. Lets take the Greek name for January (Ιανουάριος) as an example.

Without any conversion a typical date time string would be as follows:
17 Ιανουάριος 2011 (wrong)
But this is wrong in Greek, the month name should be displayed in its Geniki (Γενική) form. The elxisDateel interface will fix that:
17 Ιανουαρίου 2011 (correct)

You can create a date interface for ay language if you wish. elxisDate will auto-load the localized interface for the current language if exists. If not dates will be formatted using the general pattern.

It has been read 5078 times
elxisParameters
Previous article
elxisParameters
eFactory
Next article
eFactory

You are free to copy, distribute and transmit the articles in this site for non-commercial purposes.
Creative Commons 3.0