class CRM_Utils_Date

Date utilties

Methods

static string
format(array $date, string $separator = '', int|string $invalidDate)

Format a date by padding it with leading '0'.

static array
getAbbrWeekdayNames()

Return abbreviated weekday names according to the locale.

static array
getFullWeekdayNames()

Return full weekday names according to the locale.

static array
getAbbrMonthNames(bool $month = FALSE)

Return abbreviated month names according to the locale.

static array
getFullMonthNames()

Return full month names according to the locale.

static int
unixTime($string)

No description

static string
customFormat(string $dateString, string $format = NULL, array $dateParts = NULL)

Create a date and time string in a provided format.

static string
mysqlToIso(string $mysql)

Converts the date/datetime from MySQL format to ISO format

static string
isoToMysql(string $iso)

Converts the date/datetime from ISO format to MySQL format Note that until CRM-14986/ 4.4.7 this was required whenever the pattern $dao->find(TRUE): $dao->save(); was used to update an object with a date field was used. The DAO now checks for a '-' in date field strings & runs this function if the - appears - meaning it is likely redundant in the form & BAO layers

static bool
convertToDefaultDate(array $params, int $dateType, string $dateParam)

Converts the any given date to default date format.

static bool
isDate($date)

No description

static bool|string
currentDBDate(null $timeStamp = NULL)

No description

static bool
overdue($date, null $now = NULL)

No description

static string
getToday(array $dayParams = NULL, string $format = "Y-m-d")

Get customized today.

static bool
getRange(date $startDate, date $endDate)

Find whether today's date lies in the given range

static array
getFromTo(date $relative, $from, $to)

Get start date and end from the given relative term and unit

static int
calculateAge(date $birthDate)

Calculate Age in Years if greater than one year else in months.

static array
intervalAdd(string $unit, int $interval, array $date, bool $dontCareTime = FALSE)

Calculate next payment date according to provided unit & interval

static string|null
getDateFieldViewFormat($format)

Get the smarty view presentation mapping for the given format.

static mixed
pickBestSmartyFormat($format)

Pick the smarty format from settings that best matches the time string we have.

static array
datePluginToPHPFormats()

Map date plugin and actual format that is used by PHP.

static array
relativeToAbsolute(array $relativeTerm, int $unit)

Resolves the given relative time interval into finite time limits.

static int
calculateFiscalYear(int $fyDate, int $fyMonth)

Calculate current fiscal year based on the fiscal month and day.

static string
processDate(string $date, string $time = NULL, bool|string $returnNullString = FALSE, string $format = 'YmdHis')

Function to process date, convert to mysql format

static array
addDateMetadataToField(array $fieldMetaData, array $field)

Add the metadata about a date field to the field.

static array
getDatePickerExtra(array $field)

Get the fields required for the 'extra' parameter when adding a datepicker.

static array
getDatePickerAttributes(array $field)

Get the attributes parameters required for datepicker.

static array
setDateDefaults(string $mysqlDate = NULL, null $formatType = NULL, null $format = NULL, null $timeFormat = NULL)

Function to convert mysql to date plugin format.

static string
getDateFormat(string $formatType = NULL)

Function get date format.

static null|string
formatDate($date, $dateType)

No description

static array
getCalendarDayOfMonth()

Function to return days of the month.

Details

at line 51
static string format(array $date, string $separator = '', int|string $invalidDate)

Format a date by padding it with leading '0'.

Parameters

array $date ('Y', 'M', 'd').
string $separator The seperator to use when formatting the date.
int|string $invalidDate what to return if the date is invalid

Return Value

string formatted string for date

at line 178
static array getAbbrWeekdayNames()

Return abbreviated weekday names according to the locale.

Array will be in localized order according to 'weekBegins' setting, but array keys will always match to: 0 => Sun 1 => Mon etc.

Return Value

array 0-based array with abbreviated weekday names

at line 207
static array getFullWeekdayNames()

Return full weekday names according to the locale.

Array will be in localized order according to 'weekBegins' setting, but array keys will always match to: 0 => Sunday 1 => Monday etc.

Return Value

array 0-based array with full weekday names

at line 232
static array getAbbrMonthNames(bool $month = FALSE)

Return abbreviated month names according to the locale.

Parameters

bool $month

Return Value

array 1-based array with abbreviated month names

at line 255
static array getFullMonthNames()

Return full month names according to the locale.

Return Value

array 1-based array with full month names

at line 273
static int unixTime($string)

Parameters

$string

Return Value

int

at line 316
static string customFormat(string $dateString, string $format = NULL, array $dateParts = NULL)

Create a date and time string in a provided format.

%b - abbreviated month name ('Jan'..'Dec') %B - full month name ('January'..'December') %d - day of the month as a decimal number, 0-padded ('01'..'31') %e - day of the month as a decimal number, blank-padded (' 1'..'31') %E - day of the month as a decimal number ('1'..'31') %f - English ordinal suffix for the day of the month ('st', 'nd', 'rd', 'th') %H - hour in 24-hour format, 0-padded ('00'..'23') %I - hour in 12-hour format, 0-padded ('01'..'12') %k - hour in 24-hour format, blank-padded (' 0'..'23') %l - hour in 12-hour format, blank-padded (' 1'..'12') %m - month as a decimal number, 0-padded ('01'..'12') %M - minute, 0-padded ('00'..'60') %p - lowercase ante/post meridiem ('am', 'pm') %P - uppercase ante/post meridiem ('AM', 'PM') %Y - year as a decimal number including the century ('2005')

Parameters

string $dateString Date and time in 'YYYY-MM-DD hh:mm:ss' format.
string $format The output format.
array $dateParts An array with the desired date parts.

Return Value

string the $format-formatted $date

at line 450
static string mysqlToIso(string $mysql)

Converts the date/datetime from MySQL format to ISO format

Parameters

string $mysql Date/datetime in MySQL format.

Return Value

string date/datetime in ISO format

at line 493
static string isoToMysql(string $iso)

Converts the date/datetime from ISO format to MySQL format Note that until CRM-14986/ 4.4.7 this was required whenever the pattern $dao->find(TRUE): $dao->save(); was used to update an object with a date field was used. The DAO now checks for a '-' in date field strings & runs this function if the - appears - meaning it is likely redundant in the form & BAO layers

Parameters

string $iso Date/datetime in ISO format.

Return Value

string date/datetime in MySQL format

at line 510
static bool convertToDefaultDate(array $params, int $dateType, string $dateParam)

Converts the any given date to default date format.

Parameters

array $params Has given date-format.
int $dateType Type of date.
string $dateParam Index of params.

Return Value

bool

at line 691
static bool isDate($date)

Parameters

$date

Return Value

bool

at line 703
static bool|string currentDBDate(null $timeStamp = NULL)

Parameters

null $timeStamp

Return Value

bool|string

at line 713
static bool overdue($date, null $now = NULL)

Parameters

$date
null $now

Return Value

bool

at line 745
static string getToday(array $dayParams = NULL, string $format = "Y-m-d")

Get customized today.

This function is used for getting customized today. To get actuall today pass 'dayParams' as null. or else pass the day, month, year values as array values Example: $dayParams = array( 'day' => '25', 'month' => '10', 'year' => '2007' );

Parameters

array $dayParams of the day, month, year. Array of the day, month, year. values.
string $format Expected date format( default. format is 2007-12-21 )

Return Value

string Return the customized today's date (Y-m-d)

at line 772
static bool getRange(date $startDate, date $endDate)

Find whether today's date lies in the given range

Parameters

date $startDate Start date for the range.
date $endDate End date for the range.

Return Value

bool true if today's date is in the given date range

at line 803
static array getFromTo(date $relative, $from, $to)

Get start date and end from the given relative term and unit

Parameters

date $relative Eg: term.unit.
$from
$to

Return Value

array start date, end date

at line 827
static int calculateAge(date $birthDate)

Calculate Age in Years if greater than one year else in months.

Parameters

date $birthDate Birth Date.

Return Value

int array $results contains years or months

at line 891
static array intervalAdd(string $unit, int $interval, array $date, bool $dontCareTime = FALSE)

Calculate next payment date according to provided unit & interval

Parameters

string $unit Frequency unit like year,month, week etc.
int $interval Frequency interval.
array $date Start date of pledge.
bool $dontCareTime

Return Value

array contains new date with added interval

at line 956
static string|null getDateFieldViewFormat($format)

Get the smarty view presentation mapping for the given format.

Historically it was decided that where the view format is 'dd/mm/yy' or 'mm/dd/yy' they should be rendered using a longer date format. This is likely as much to do with the earlier date widget being unable to handle some formats as usablity. However, we continue to respect this.

Parameters

$format Given format ( eg 'M Y', 'Y M' ).

Return Value

string|null Smarty translation of the date format. Null is also valid and is translated according to the available parts at the smarty layer.

at line 978
static mixed pickBestSmartyFormat($format)

Pick the smarty format from settings that best matches the time string we have.

For view purposes we historically use the setting that most closely matches the data in the format from our settings, as opposed to the setting configured for the field.

Parameters

$format

Return Value

mixed

at line 996
static array datePluginToPHPFormats()

Map date plugin and actual format that is used by PHP.

Return Value

array

at line 1030
static array relativeToAbsolute(array $relativeTerm, int $unit)

Resolves the given relative time interval into finite time limits.

Parameters

array $relativeTerm Relative time frame like this, previous, etc.
int $unit Frequency unit like year, month, week etc.

Return Value

array start date and end date for the relative time frame

at line 1742
static int calculateFiscalYear(int $fyDate, int $fyMonth)

Calculate current fiscal year based on the fiscal month and day.

Parameters

int $fyDate Fiscal start date.
int $fyMonth Fiscal Start Month.

Return Value

int $fy Current Fiscl Year

at line 1776
static string processDate(string $date, string $time = NULL, bool|string $returnNullString = FALSE, string $format = 'YmdHis')

Function to process date, convert to mysql format

Parameters

string $date Date string.
string $time Time string.
bool|string $returnNullString 'null' needs to be returned so that db oject will set null in db
string $format Expected return date format.( default is mysql ).

Return Value

string date format that is excepted by mysql

at line 1800
static array addDateMetadataToField(array $fieldMetaData, array $field)

Add the metadata about a date field to the field.

This metadata will work with the call $form->add('datepicker', ...

Parameters

array $fieldMetaData
array $field

Return Value

array

at line 1828
static array getDatePickerExtra(array $field)

Get the fields required for the 'extra' parameter when adding a datepicker.

Parameters

array $field

Return Value

array

at line 1853
static array getDatePickerAttributes(array $field)

Get the attributes parameters required for datepicker.

Parameters

array $field Field metadata

Return Value

array Array ready to pass to $this->addForm('datepicker' as attributes.

at line 1881
static array setDateDefaults(string $mysqlDate = NULL, null $formatType = NULL, null $format = NULL, null $timeFormat = NULL)

Function to convert mysql to date plugin format.

Parameters

string $mysqlDate Date string.
null $formatType
null $format
null $timeFormat

Return Value

array and time

at line 1948
static string getDateFormat(string $formatType = NULL)

Function get date format.

Parameters

string $formatType Date name e.g. birth.

Return Value

string

at line 1969
static null|string formatDate($date, $dateType)

Parameters

$date
$dateType

Return Value

null|string

at line 2018
static array getCalendarDayOfMonth()

Function to return days of the month.

Return Value

array