include/TimeDate.php
\TimeDate
New Time & Date handling class
- Api
- Migration notes: - to_db_time() requires either full datetime or time, won't work with just date The reason is that it's not possible to know if short string has only date or only time, and it makes more sense to assume time for the time conversion function.
Constants
Properties

bool
$allow_cache= 'true'
Allow returning cached now() value If false, new system time is checked each time now() is required If true, same value is returned for whole request.
Also, current user's timezone is cached.
trueDetails- Type
- bool

bool
$always_db= 'false'
Always consider user TZ to be GMT and date format DB format - for SOAP etc.
falseDetails- Type
- bool

$date_expressions= 'array(
'yesterday' => array("-1 day", "-1 day"),
'today' => array("", ""),
'tomorrow' => array("+1 day", "+1 day"),
'last_7_days' => array("-6 days", ""),
'next_7_days' => array("", "+6 days"),
'last_30_days' => array("-29 days", ""),
'next_30_days' => array("", "+29 days"),
)'
array(
'yesterday' => array("-1 day", "-1 day"),
'today' => array("", ""),
'tomorrow' => array("+1 day", "+1 day"),
'last_7_days' => array("-6 days", ""),
'next_7_days' => array("", "+6 days"),
'last_30_days' => array("-29 days", ""),
'next_30_days' => array("", "+29 days"),
)Details- Type
- n/a

array
$format_to_regexp= 'array(
'a' => '[ ]*[ap]m',
'A' => '[ ]*[AP]M',
'd' => '[0-9]{1,2}',
'j' => '[0-9]{1,2}',
'h' => '[0-9]{1,2}',
'H' => '[0-9]{1,2}',
'g' => '[0-9]{1,2}',
'G' => '[0-9]{1,2}',
'i' => '[0-9]{1,2}',
'm' => '[0-9]{1,2}',
'n' => '[0-9]{1,2}',
'Y' => '[0-9]{4}',
's' => '[0-9]{1,2}',
'F' => '\w+',
"M" => '[\w]{1,3}',
)'
Regexp for matching format elements
array(
'a' => '[ ]*[ap]m',
'A' => '[ ]*[AP]M',
'd' => '[0-9]{1,2}',
'j' => '[0-9]{1,2}',
'h' => '[0-9]{1,2}',
'H' => '[0-9]{1,2}',
'g' => '[0-9]{1,2}',
'G' => '[0-9]{1,2}',
'i' => '[0-9]{1,2}',
'm' => '[0-9]{1,2}',
'n' => '[0-9]{1,2}',
'Y' => '[0-9]{4}',
's' => '[0-9]{1,2}',
'F' => '\w+',
"M" => '[\w]{1,3}',
)Details- Type
- array

array
$format_to_str= 'array(
// date
'Y' => '%Y',
'm' => '%m',
'M' => '%b',
'F' => '%B',
'n' => '%m',
'd' => '%d',
//'j' => '%e',
// time
'a' => '%P',
'A' => '%p',
'h' => '%I',
'H' => '%H',
//'g' => '%l',
//'G' => '%H',
'i' => '%M',
's' => '%S',
)'
Relation between date() and strftime() formats
array(
// date
'Y' => '%Y',
'm' => '%m',
'M' => '%b',
'F' => '%B',
'n' => '%m',
'd' => '%d',
//'j' => '%e',
// time
'a' => '%P',
'A' => '%p',
'h' => '%I',
'H' => '%H',
//'g' => '%l',
//'G' => '%H',
'i' => '%M',
's' => '%S',
)Details- Type
- array
Methods

AMPMMenu(
string $prefix, string $date, string $attrs
=
''
)
:
stringAMPMMenu This method renders a SELECT HTML form element based on the user's time format preferences, with give date's value highlighted.
If user's prefs have no AM/PM string, returns empty string.
| Name | Type | Description |
|---|---|---|
| $prefix | string | Prefix for SELECT |
| $date | string | Date in display format |
| $attrs | string | Additional attributes for SELECT |
| Type | Description |
|---|---|
| string | SELECT HTML |
- Deprecated
- Todo
- There is hardcoded HTML in here that does not allow for localization of the AM/PM am/pm Strings in this drop down menu. Also, perhaps change to the substr_count function calls to strpos TODO: Remove after full switch to fields

__construct(
\User $user
=
null
)
:
voidCreate TimeDate handler
| Name | Type | Description |
|---|---|---|
| $user | \User | User to work with, default if current user |

_convert(
string $date, string $fromFormat, \DateTimeZone $fromTZ, string $toFormat, \DateTimeZone | null $toTZ, bool $expand
=
false
)
:
stringBasic conversion function
Converts between two string dates in different formats and timezones
| Name | Type | Description |
|---|---|---|
| $date | string | |
| $fromFormat | string | |
| $fromTZ | \DateTimeZone | |
| $toFormat | string | |
| $toTZ | \DateTimeZone | null | |
| $expand | bool | If string lacks time, expand it to include time |
| Type | Description |
|---|---|
| string |

_getUserTZ(
\User $user
=
null
)
:
\DateTimeZoneGet timezone for the specified user
| Name | Type | Description |
|---|---|---|
| $user | \User | User object, default is current user |
| Type | Description |
|---|---|
| \DateTimeZone |

_get_midnight(
string $format
=
null
)
:
stringGet string defining midnight in current user's format
| Name | Type | Description |
|---|---|---|
| $format | string | Time format to use |
| Type | Description |
|---|---|
| string |

adjustmentForUserTimeZone(
)
:
integer| Type | Description |
|---|---|
| integer | number of minutes to adjust a time by to get the appropriate time for the user |
- Deprecated
- for public use this returns the adjustment for a user against the server time

asDb(
\DateTime $date
)
:
stringFormat DateTime object as DB datetime
| Name | Type | Description |
|---|---|---|
| $date | \DateTime |
| Type | Description |
|---|---|
| string |

asDbDate(
\DateTime $date, boolean $tz
=
false
)
:
stringFormat DateTime object as DB date Note: by default does not convert TZ!
| Name | Type | Description |
|---|---|---|
| $date | \DateTime | |
| $tz | boolean | Perform TZ conversion? |
| Type | Description |
|---|---|
| string |

asDbTime(
\DateTime $date
)
:
stringFormat DateTime object as DB time
| Name | Type | Description |
|---|---|---|
| $date | \DateTime |
| Type | Description |
|---|---|
| string |

asDbType(
\DateTime $date, string $type
)
:
stringFormat date as DB-formatted field type
| Name | Type | Description |
|---|---|---|
| $date | \DateTime | |
| $type | string | Field type - date, time, datetime[combo] |
| Type | Description |
|---|---|
| string | Formatted date |

asUserDate(
\DateTime $date, boolean $tz
=
false, \User $user
=
null
)
:
stringFormat DateTime object as user date Note: by default does not convert TZ!

asUserType(
\DateTime $date, string $type, \User $user
=
null
)
:
stringFormat date as user-formatted field type

convert_to_gmt_datetime(
$olddatetime
)
:
string| Name | Type | Description |
|---|---|---|
| $olddatetime |
| Type | Description |
|---|---|
| string |
- Deprecated
- for public use assumes that olddatetime is in Y-m-d H:i:s format

expandDate(
string $date, string $format
)
:
stringExpand date format by adding midnight to it Note: date is assumed to be in target format already
| Name | Type | Description |
|---|---|---|
| $date | string | |
| $format | string | Target format |
| Type | Description |
|---|---|
| string |

expandTime(
string $date, string $format, \DateTimeZone $tz
)
:
stringExpand time format by adding today to it Note: time is assumed to be in target format already
| Name | Type | Description |
|---|---|---|
| $date | string | |
| $format | string | Target format |
| $tz | \DateTimeZone |
| Type | Description |
|---|---|
| string |

fromDb(
string $date
)
:
\SugarDateTimeGet DateTime from DB datetime string
| Name | Type | Description |
|---|---|---|
| $date | string |
| Type | Description |
|---|---|
| \SugarDateTime |

fromDbDate(
string $date
)
:
\SugarDateTimeGet DateTime from DB date string
| Name | Type | Description |
|---|---|---|
| $date | string |
| Type | Description |
|---|---|
| \SugarDateTime |

fromDbFormat(
string $date, string $format
)
:
\SugarDateTimeGet DateTime from DB datetime string using non-standard format
Non-standard format usually would be only date, only time, etc.
| Name | Type | Description |
|---|---|---|
| $date | string | |
| $format | string | format to accept |
| Type | Description |
|---|---|
| \SugarDateTime |

fromDbType(
string $date, string $type
)
:
\SugarDateTimeCreate a date from a certain type of field in DB format The types are: date, time, datatime[combo]
| Name | Type | Description |
|---|---|---|
| $date | string | the datetime string |
| $type | string | string type |
| Type | Description |
|---|---|
| \SugarDateTime |

fromString(
string $date, \User $user
=
null
)
:
\SugarDateTimeCreate a date object from any string
Same formats accepted as for DateTime ctor
| Name | Type | Description |
|---|---|---|
| $date | string | |
| $user | \User |
| Type | Description |
|---|---|
| \SugarDateTime |

fromTimeArray(
array $time
)
:
\SugarDateTimeCreate datetime object from calendar array
| Name | Type | Description |
|---|---|---|
| $time | array |
| Type | Description |
|---|---|
| \SugarDateTime |

fromTimestamp(
\interger | string $ts
)
:
\SugarDateTimeCreate DateTime from timestamp
| Name | Type | Description |
|---|---|---|
| $ts | \interger | string |
| Type | Description |
|---|---|
| \SugarDateTime |

fromUser(
string $date, \User $user
=
null
)
:
\SugarDateTimeGet DateTime from user datetime string
| Name | Type | Description |
|---|---|---|
| $date | string | |
| $user | \User |
| Type | Description |
|---|---|
| \SugarDateTime |

fromUserDate(
string $date, bool $convert_tz
=
false, \User $user
=
null
)
:
\SugarDateTimeGet DateTime from user date string Usually for calendar-related functions like holidays Note: by default does not convert tz!
| Name | Type | Description |
|---|---|---|
| $date | string | |
| $convert_tz | bool | perform TZ converson? |
| $user | \User |
| Type | Description |
|---|---|
| \SugarDateTime |

fromUserTime(
string $date, \User $user
=
null
)
:
\SugarDateTimeGet DateTime from user time string
| Name | Type | Description |
|---|---|---|
| $date | string | |
| $user | \User |
| Type | Description |
|---|---|
| \SugarDateTime |

fromUserType(
string $date, string $type, \User $user
=
null
)
:
\SugarDateTimeCreate a date from a certain type of field in user format The types are: date, time, datatime[combo]
| Name | Type | Description |
|---|---|---|
| $date | string | the datetime string |
| $type | string | string type |
| $user | \User |
| Type | Description |
|---|---|
| \SugarDateTime |

getDSTRange(
$year, string $zone
=
null
)
:
array| Name | Type | Description |
|---|---|---|
| $year | ||
| $zone | string |
| Type | Description |
|---|---|
| array |
- Deprecated
- for public use get timezone start & end

getDatePart(
string $datetime
)
:
stringReturns the date portion of a datetime string
| Name | Type | Description |
|---|---|---|
| $datetime | string |
| Type | Description |
|---|---|
| string |

getDayStartEndGMT(
string | \DateTime $date, \User $user
=
null
)
:
arrayReturns start and end of a certain local date in GMT Example: for May 19 in PDT start would be 2010-05-19 07:00:00, end would be 2010-05-20 06:59:59

getNow(
bool $userTz
=
false
)
:
\SugarDateTimeGet 'now' DateTime object
| Name | Type | Description |
|---|---|---|
| $userTz | bool | return in user timezone? |
| Type | Description |
|---|---|
| \SugarDateTime |

getTimePart(
string $datetime
)
:
stringReturns the time portion of a datetime string
| Name | Type | Description |
|---|---|---|
| $datetime | string |
| Type | Description |
|---|---|
| string |

getTimezoneList(
)
:
arrayGet list of all timezones in the system
| Type | Description |
|---|---|
| array |

getUserTimeZone(
\User $user
=
null
)
:
array| Name | Type | Description |
|---|---|---|
| $user | \User |
| Type | Description |
|---|---|
| array |
- Deprecated
- for public use get user timezone info

getUserUTCOffset(
\User $user
=
null, \DateTime $time
=
null
)
:
intReturns the offset from user's timezone to GMT

get_cal_date_format(
)
:
stringGet user date format in Javascript form
| Type | Description |
|---|---|
| string |

get_cal_date_time_format(
)
:
stringGet user date&time format in Javascript form
| Type | Description |
|---|---|
| string |

get_cal_time_format(
)
:
stringGet user time format in Javascript form
| Type | Description |
|---|---|
| string |

get_date_format(
\User $user
=
null
)
:
stringGet user date format.
| Name | Type | Description |
|---|---|---|
| $user | \User | user object, current user if not specified |
| Type | Description |
|---|---|
| string |
- Todo
- add caching

get_date_time_format(
\User $user
=
null
)
:
stringGet user datetime format.
| Name | Type | Description |
|---|---|---|
| $user | \User | user object, current user if not specified |
| Type | Description |
|---|---|
| string |

get_date_time_format_cache_key(
$user
)
:
stringRetrieve the cache key used for user date/time formats
| Name | Type | Description |
|---|---|---|
| $user |
| Type | Description |
|---|---|
| string |

get_default_midnight(
)
:
\TimeGet midnight (start of the day) in local time format
| Type | Description |
|---|---|
| \Time | string |

get_first_day_of_week(
\User $user
=
null
)
:
intGet user's first day of week setting.
| Name | Type | Description |
|---|---|---|
| $user | \User | user object, current user if not specified |
| Type | Description |
|---|---|
| int | Day, 0 = Sunday, 1 = Monday, etc... |

get_gmt_db_date(
)
:
string| Type | Description |
|---|---|
| string |
- Deprecated
- for public use Get current GMT date in DB format

get_gmt_db_datetime(
)
:
string| Type | Description |
|---|---|
| string |
- Deprecated
- for public use Get current GMT datetime in DB format

get_javascript_validation(
)
:
stringGet Javascript variables setup for user date format validation
| Type | Description |
|---|---|
| string | JS code |
- Deprecated
- moved to SugarView

get_regular_expression(
string $format
)
:
stringCreate regexp from datetime format
| Name | Type | Description |
|---|---|---|
| $format | string |
| Type | Description |
|---|---|
| string | Regular expression string |

get_time_format(
\User $user
=
null
)
:
stringGet user time format.
| Name | Type | Description |
|---|---|---|
| $user | \User | user object, current user if not specified |
| Type | Description |
|---|---|
| string |
- Todo
- add caching

get_user_date_format(
)
:
stringGet user format in JS form TODO: Remove after full switch to fields
| Type | Description |
|---|---|
| string |

get_user_time_format(
)
:
stringGet user time format example TODO: Remove after full switch to fields
| Type | Description |
|---|---|
| string |
- Deprecated

guessTimezone(
int $userOffset
=
0
)
:
stringGuess the timezone for the current user
| Name | Type | Description |
|---|---|---|
| $userOffset | int | Offset from GMT in minutes |
| Type | Description |
|---|---|
| string |

handleOffsetMax(
$date
)
:
array| Name | Type | Description |
|---|---|---|
| $date |
| Type | Description |
|---|---|
| array |
- Deprecated
- for public use this method will take an input $date variable (expecting Y-m-d format) and get the GMT equivalent - with an hour-level granularity : return the max value of a given locale's date+time in GMT metrics (i.e., if in PDT, "2005-01-01 23:59:59" would be "2005-01-02 06:59:59" in GMT metrics)

handle_offset(
\$date $date, \$format $format, \$to $to
=
true, \$user $user
=
null, \$usetimezone $usetimezone
=
null
)
:
string| Name | Type | Description |
|---|---|---|
| $date | \$date | string date/time formatted in user's selected format |
| $format | \$format | string destination format value as passed to PHP's date() funtion |
| $to | \$to | boolean |
| $user | \$user | object user object from which Timezone and DST |
| $usetimezone | \$usetimezone | string timezone name values will be derived |
| Type | Description |
|---|---|
| string | date formatted and adjusted for TZ and DST |
- Deprecated
- for public use handles offset values for Timezones and DST

httpTime(
int | null $ts
=
null
)
:
stringPrint timestamp in RFC2616 format:
| Name | Type | Description |
|---|---|---|
| $ts | int | null | Null means current ts |
| Type | Description |
|---|---|
| string |

merge_date_time(
string $date, string $time
)
:
stringMake one datetime string from date string and time string
| Name | Type | Description |
|---|---|---|
| $date | string | |
| $time | string |
| Type | Description |
|---|---|
| string | New datetime string |

merge_time_meridiem(
string $date, string $format, string $mer
)
:
stringMerge time without am/pm with am/pm string
| Name | Type | Description |
|---|---|---|
| $date | string | |
| $format | string | User time format |
| $mer | string |
| Type | Description |
|---|---|
| string |
- Deprecated
- for public use
- TODO
- find better way to do this!

parseDateRange(
string $range, \User $user
=
null, bool $adjustForTimezone
=
true
)
:
arrayParse date range expression Returns beginning and end of the range as a date
| Name | Type | Description |
|---|---|---|
| $range | string | |
| $user | \User | |
| $adjustForTimezone | bool | Do we need to adjust for timezone? |
| Type | Description |
|---|---|
| array | of two Date objects, start & end |

setAlwaysDb(
bool $flag
=
true
)
:
\TimeDateSet flag specifying we should always use DB format
| Name | Type | Description |
|---|---|---|
| $flag | bool |
| Type | Description |
|---|---|
| \TimeDate |

splitTime(
string $date, string $format
)
:
arraySplits time in given format into components
Components: h, m, s, a (am/pm) if format requires it If format has am/pm, hour is 12-based, otherwise 24-based
| Name | Type | Description |
|---|---|---|
| $date | string | |
| $format | string |
| Type | Description |
|---|---|
| array |

split_date_time(
string $datetime
)
:
arraySplit datetime string into date & time
| Name | Type | Description |
|---|---|---|
| $datetime | string |
| Type | Description |
|---|---|
| array |

swap_formats(
string $date, string $from, string $to
)
:
string| Name | Type | Description |
|---|---|---|
| $date | string | |
| $from | string | |
| $to | string |
| Type | Description |
|---|---|
| string |
- Deprecated
- for public use Convert date from one format to another

timeSeparatorFormat(
string $timeformat
)
:
\stringSFind out format's time separator
| Name | Type | Description |
|---|---|---|
| $timeformat | string | Time format |
| Type | Description |
|---|---|
| \stringS |

to_db(
string $date
)
:
stringConvert date from local datetime to GMT-based DB datetime
Includes TZ conversion.
| Name | Type | Description |
|---|---|---|
| $date | string |
| Type | Description |
|---|---|
| string | Datetime in DB format |

to_db_date(
string $date, bool $convert_tz
=
true
)
:
stringConvert local datetime to DB date
TZ conversion depends on parameter. If false, only format conversion is performed.
| Name | Type | Description |
|---|---|---|
| $date | string | Local date |
| $convert_tz | bool | Should time and TZ be taken into account? |
| Type | Description |
|---|---|
| string | Date in DB format |

to_db_date_time(
string $date, string $time
)
:
arrayTakes a Date & Time value in local format and converts them to DB format No TZ conversion!
| Name | Type | Description |
|---|---|---|
| $date | string | |
| $time | string |
| Type | Description |
|---|---|
| array | Date & time in DB format |

to_db_time(
string $date, bool $convert_tz
=
true
)
:
stringConvert local datetime to DB time
TZ conversion depends on parameter. If false, only format conversion is performed.
| Name | Type | Description |
|---|---|---|
| $date | string | Local date |
| $convert_tz | bool | Should time and TZ be taken into account? |
| Type | Description |
|---|---|
| string | Time in DB format |

to_display(
string $date, string $from, string $to
)
:
stringConvert date from format to format
No TZ conversion is performed!
| Name | Type | Description |
|---|---|---|
| $date | string | |
| $from | string | Source format |
| $to | string | Destination format |
| Type | Description |
|---|---|
| string | Converted date |

to_display_date(
string $date, bool $convert_tz
=
true
)
:
stringConverts DB date string to local date string
TZ conversion depens on parameter
| Name | Type | Description |
|---|---|---|
| $date | string | Date in DB format |
| $convert_tz | bool | Perform TZ conversion? |
| Type | Description |
|---|---|
| string | Date in user-defined format |

to_display_date_time(
string $date, bool $meridiem
=
true, bool $convert_tz
=
true, \User $user
=
null
)
:
stringConvert DB datetime to local datetime
TZ conversion is controlled by parameter
| Name | Type | Description |
|---|---|---|
| $date | string | Original date in DB format |
| $meridiem | bool | Ignored for BC |
| $convert_tz | bool | Perform TZ conversion? |
| $user | \User | User owning the conversion formats |
| Type | Description |
|---|---|
| string | Date in display format |

to_display_time(
string $date, bool $meridiem
=
true, bool $convert_tz
=
true
)
:
stringConverts DB time string to local time string
TZ conversion depends on parameter
| Name | Type | Description |
|---|---|---|
| $date | string | Time in DB format |
| $meridiem | bool | |
| $convert_tz | bool | Perform TZ conversion? |
| Type | Description |
|---|---|
| string | Time in user-defined format |

tzName(
string | \DateTimeZone $name
)
:
stringGet display name for a certain timezone Note: it uses current date for GMT offset, so it may be not suitable for displaying generic dates
| Name | Type | Description |
|---|---|---|
| $name | string | \DateTimeZone | TZ name |
| Type | Description |
|---|---|
| string |

userTimezone(
\User $user
=
null
)
:
stringGet the name of the timezone for the user
| Name | Type | Description |
|---|---|---|
| $user | \User | User, default - current user |
| Type | Description |
|---|---|
| string |