include/TimeDate.php

Show: inherited
Table of Contents

\TimeDate

Package: SugarCRM

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

Constant  DB_DATE_FORMAT = 'Y-m-d'
Constant  DB_TIME_FORMAT = 'H:i:s'
Constant  DB_DATETIME_FORMAT = 'Y-m-d H:i:s'
Constant  RFC2616_FORMAT = 'D, d M Y H:i:s \G\M\T'
Constant  SECONDS_IN_A_DAY = 86400

Properties

Propertypublicbool  $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.

Default valuetrueDetails
Type
bool
Propertyprotectedbool  $always_db= 'false'

Always consider user TZ to be GMT and date format DB format - for SOAP etc.

Default valuefalseDetails
Type
bool
Propertyprotectedstring  $current_user_id= ''

Current user's ID

Details
Type
string
Propertyprotected\DateTimeZone  $current_user_tz= ''

Current user's TZ

Details
Type
\DateTimeZone
Propertyprotected  $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"), )'
Default valuearray( '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
Propertypublic  $dbDayFormat= 'self::DB_DATE_FORMAT'
Default valueself::DB_DATE_FORMATDetails
Type
n/a
Propertypublic  $dbTimeFormat= 'self::DB_TIME_FORMAT'
Default valueself::DB_TIME_FORMATDetails
Type
n/a
Propertyprotectedarray  $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}', )'
static

Regexp for matching format elements

Default valuearray( '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
Propertypublicarray  $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', )'
static

Relation between date() and strftime() formats

Default valuearray( // 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
Propertyprotected\DateTimeZone  $gmtTimezone= ''
static

GMT timezone object

Details
Type
\DateTimeZone
Propertyprotected\SugarDateTime  $now= ''

Current time

Details
Type
\SugarDateTime
Propertyprotectedstring  $time_separator= ''

Separator for current user time format

Details
Type
string
Propertyprotected\TimeDate  $timedate= ''
static

Global instance of TimeDate

Details
Type
\TimeDate
Propertyprotected\User  $user= ''

The current user

Details
Type
\User

Methods

methodpublicAMPMMenu( string $prefix, string $date, string $attrs = '' ) : string

AMPMMenu 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.

Parameters
Name Type Description
$prefix string

Prefix for SELECT

$date string

Date in display format

$attrs string

Additional attributes for SELECT

Returns
Type Description
string SELECT HTML
Details
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  
methodpublic__construct( \User $user = null ) : void

Create TimeDate handler

Parameters
Name Type Description
$user \User

User to work with, default if current user

methodprotected_convert( string $date, string $fromFormat, \DateTimeZone $fromTZ, string $toFormat, \DateTimeZone | null $toTZ, bool $expand = false ) : string

Basic conversion function

Converts between two string dates in different formats and timezones

Parameters
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

Returns
Type Description
string
methodprotected_getUserTZ( \User $user = null ) : \DateTimeZone

Get timezone for the specified user

Parameters
Name Type Description
$user \User

User object, default is current user

Returns
Type Description
\DateTimeZone
methodprotected_get_midnight( string $format = null ) : string

Get string defining midnight in current user's format

Parameters
Name Type Description
$format string

Time format to use

Returns
Type Description
string
methodpublicadjustmentForUserTimeZone( ) : integer

Returns
Type Description
integer number of minutes to adjust a time by to get the appropriate time for the user
Details
Deprecated
for public use this returns the adjustment for a user against the server time  
methodpublicasDb( \DateTime $date ) : string

Format DateTime object as DB datetime

Parameters
Name Type Description
$date \DateTime
Returns
Type Description
string
methodpublicasDbDate( \DateTime $date, boolean $tz = false ) : string

Format DateTime object as DB date Note: by default does not convert TZ!

Parameters
Name Type Description
$date \DateTime
$tz boolean

Perform TZ conversion?

Returns
Type Description
string
methodpublicasDbTime( \DateTime $date ) : string

Format DateTime object as DB time

Parameters
Name Type Description
$date \DateTime
Returns
Type Description
string
methodpublicasDbType( \DateTime $date, string $type ) : string

Format date as DB-formatted field type

Parameters
Name Type Description
$date \DateTime
$type string

Field type - date, time, datetime[combo]

Returns
Type Description
string Formatted date
methodpublicasUser( \DateTime $date, \User $user = null ) : string

Format DateTime object as user datetime

Parameters
Name Type Description
$date \DateTime
$user \User
Returns
Type Description
string
methodpublicasUserDate( \DateTime $date, boolean $tz = false, \User $user = null ) : string

Format DateTime object as user date Note: by default does not convert TZ!

Parameters
Name Type Description
$date \DateTime
$tz boolean

Perform TZ conversion?

$user \User
Returns
Type Description
string
methodpublicasUserTime( \DateTime $date, \User $user = null ) : string

Format DateTime object as user time

Parameters
Name Type Description
$date \DateTime
$user \User
Returns
Type Description
string
methodpublicasUserTs( \DateTime $date, \User $user = null ) : int

Produce timestamp offset by user's timezone

So if somebody converts it to format assuming GMT, it would actually display user's time. This is used by Javascript.

Parameters
Name Type Description
$date \DateTime
$user \User
Returns
Type Description
int
methodpublicasUserType( \DateTime $date, string $type, \User $user = null ) : string

Format date as user-formatted field type

Parameters
Name Type Description
$date \DateTime
$type string

Field type - date, time, datetime[combo]

$user \User
Returns
Type Description
string
methodpublicclearCache( ) : void

Clears all cached data regarding current user

methodpublicconvert_to_gmt_datetime(  $olddatetime ) : string

Parameters
Name Type Description
$olddatetime
Returns
Type Description
string
Details
Deprecated
for public use assumes that olddatetime is in Y-m-d H:i:s format  
methodpublicexpandDate( string $date, string $format ) : string

Expand date format by adding midnight to it Note: date is assumed to be in target format already

Parameters
Name Type Description
$date string
$format string

Target format

Returns
Type Description
string
methodpublicexpandTime( string $date, string $format, \DateTimeZone $tz ) : string

Expand time format by adding today to it Note: time is assumed to be in target format already

Parameters
Name Type Description
$date string
$format string

Target format

$tz \DateTimeZone
Returns
Type Description
string
methodpublicfromDb( string $date ) : \SugarDateTime

Get DateTime from DB datetime string

Parameters
Name Type Description
$date string
Returns
Type Description
\SugarDateTime
methodpublicfromDbDate( string $date ) : \SugarDateTime

Get DateTime from DB date string

Parameters
Name Type Description
$date string
Returns
Type Description
\SugarDateTime
methodpublicfromDbFormat( string $date, string $format ) : \SugarDateTime

Get DateTime from DB datetime string using non-standard format

Non-standard format usually would be only date, only time, etc.

Parameters
Name Type Description
$date string
$format string

format to accept

Returns
Type Description
\SugarDateTime
methodpublicfromDbType( string $date, string $type ) : \SugarDateTime

Create a date from a certain type of field in DB format The types are: date, time, datatime[combo]

Parameters
Name Type Description
$date string

the datetime string

$type string

string type

Returns
Type Description
\SugarDateTime
methodpublicfromString( string $date, \User $user = null ) : \SugarDateTime

Create a date object from any string

Same formats accepted as for DateTime ctor

Parameters
Name Type Description
$date string
$user \User
Returns
Type Description
\SugarDateTime
methodpublicfromTimeArray( array $time ) : \SugarDateTime

Create datetime object from calendar array

Parameters
Name Type Description
$time array
Returns
Type Description
\SugarDateTime
methodpublicfromTimestamp( \interger | string $ts ) : \SugarDateTime

Create DateTime from timestamp

Parameters
Name Type Description
$ts \interger | string
Returns
Type Description
\SugarDateTime
methodpublicfromUser( string $date, \User $user = null ) : \SugarDateTime

Get DateTime from user datetime string

Parameters
Name Type Description
$date string
$user \User
Returns
Type Description
\SugarDateTime
methodpublicfromUserDate( string $date, bool $convert_tz = false, \User $user = null ) : \SugarDateTime

Get DateTime from user date string Usually for calendar-related functions like holidays Note: by default does not convert tz!

Parameters
Name Type Description
$date string
$convert_tz bool

perform TZ converson?

$user \User
Returns
Type Description
\SugarDateTime
methodpublicfromUserTime( string $date, \User $user = null ) : \SugarDateTime

Get DateTime from user time string

Parameters
Name Type Description
$date string
$user \User
Returns
Type Description
\SugarDateTime
methodpublicfromUserType( string $date, string $type, \User $user = null ) : \SugarDateTime

Create a date from a certain type of field in user format The types are: date, time, datatime[combo]

Parameters
Name Type Description
$date string

the datetime string

$type string

string type

$user \User
Returns
Type Description
\SugarDateTime
methodpublicgetDSTRange(  $year, string $zone = null ) : array

Parameters
Name Type Description
$year
$zone string
Returns
Type Description
array
Details
Deprecated
for public use get timezone start & end  
methodpublicgetDatePart( string $datetime ) : string

Returns the date portion of a datetime string

Parameters
Name Type Description
$datetime string
Returns
Type Description
string
methodpublicgetDayStartEndGMT( string | \DateTime $date, \User $user = null ) : array

Returns 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

Parameters
Name Type Description
$date string | \DateTime

Date in any suitable format

$user \User
Returns
Type Description
array Start & end date in start, startdate, starttime, end, enddate, endtime
methodpublicgetInstance( ) : \TimeDate
static

Get TimeDate instance

Returns
Type Description
\TimeDate
methodpublicgetNow( bool $userTz = false ) : \SugarDateTime

Get 'now' DateTime object

Parameters
Name Type Description
$userTz bool

return in user timezone?

Returns
Type Description
\SugarDateTime
methodpublicgetTimePart( string $datetime ) : string

Returns the time portion of a datetime string

Parameters
Name Type Description
$datetime string
Returns
Type Description
string
methodpublicgetTimezoneList( ) : array
static

Get list of all timezones in the system

Returns
Type Description
array
methodpublicgetUserTimeZone( \User $user = null ) : array

Parameters
Name Type Description
$user \User
Returns
Type Description
array
Details
Deprecated
for public use get user timezone info  
methodpublicgetUserUTCOffset( \User $user = null, \DateTime $time = null ) : int

Returns the offset from user's timezone to GMT

Parameters
Name Type Description
$user \User
$time \DateTime

When the offset is taken, default is now

Returns
Type Description
int Offset in minutes
methodpublicget_cal_date_format( ) : string

Get user date format in Javascript form

Returns
Type Description
string
methodpublicget_cal_date_time_format( ) : string

Get user date&time format in Javascript form

Returns
Type Description
string
methodpublicget_cal_time_format( ) : string

Get user time format in Javascript form

Returns
Type Description
string
methodpublicget_date_format( \User $user = null ) : string

Get user date format.

Parameters
Name Type Description
$user \User

user object, current user if not specified

Returns
Type Description
string
Details
Todo
add caching  
methodpublicget_date_time_format( \User $user = null ) : string

Get user datetime format.

Parameters
Name Type Description
$user \User

user object, current user if not specified

Returns
Type Description
string
methodpublicget_date_time_format_cache_key(  $user ) : string

Retrieve the cache key used for user date/time formats

Parameters
Name Type Description
$user
Returns
Type Description
string
methodpublicget_db_date_format( ) : string

Get DB date format

Returns
Type Description
string
methodpublicget_db_date_time_format( ) : string

Get DB datetime format

Returns
Type Description
string
methodpublicget_db_time_format( ) : string

Get DB time format

Returns
Type Description
string
methodpublicget_default_midnight( ) : \Time

Get midnight (start of the day) in local time format

Returns
Type Description
\Time string
methodpublicget_first_day_of_week( \User $user = null ) : int

Get user's first day of week setting.

Parameters
Name Type Description
$user \User

user object, current user if not specified

Returns
Type Description
int Day, 0 = Sunday, 1 = Monday, etc...
methodpublicget_gmt_db_date( ) : string

Returns
Type Description
string
Details
Deprecated
for public use Get current GMT date in DB format  
methodpublicget_gmt_db_datetime( ) : string

Returns
Type Description
string
Details
Deprecated
for public use Get current GMT datetime in DB format  
methodpublicget_javascript_validation( ) : string

Get Javascript variables setup for user date format validation

Returns
Type Description
string JS code
Details
Deprecated
moved to SugarView  
methodpublicget_regular_expression( string $format ) : string
static

Create regexp from datetime format

Parameters
Name Type Description
$format string
Returns
Type Description
string Regular expression string
methodpublicget_time_format( \User $user = null ) : string

Get user time format.

Parameters
Name Type Description
$user \User

user object, current user if not specified

Returns
Type Description
string
Details
Todo
add caching  
methodpublicget_user_date_format( ) : string

Get user format in JS form TODO: Remove after full switch to fields

Returns
Type Description
string
methodpublicget_user_time_format( ) : string

Get user time format example TODO: Remove after full switch to fields

Returns
Type Description
string
Details
Deprecated
 
methodpublicguessTimezone( int $userOffset = 0 ) : string
static

Guess the timezone for the current user

Parameters
Name Type Description
$userOffset int

Offset from GMT in minutes

Returns
Type Description
string
methodpublichandleOffsetMax(  $date ) : array

Parameters
Name Type Description
$date
Returns
Type Description
array
Details
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)  
methodpublichandle_offset( \$date $date, \$format $format, \$to $to = true, \$user $user = null, \$usetimezone $usetimezone = null ) : string

Parameters
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

Returns
Type Description
string date formatted and adjusted for TZ and DST
Details
Deprecated
for public use handles offset values for Timezones and DST  
methodpublichttpTime( int | null $ts = null ) : string
static

Print timestamp in RFC2616 format:

Parameters
Name Type Description
$ts int | null

Null means current ts

Returns
Type Description
string
methodpublicisAlwaysDb( ) : bool

Get "always use DB format" flag

Returns
Type Description
bool
methodpublicmerge_date_time( string $date, string $time ) : string

Make one datetime string from date string and time string

Parameters
Name Type Description
$date string
$time string
Returns
Type Description
string New datetime string
methodpublicmerge_time_meridiem( string $date, string $format, string $mer ) : string

Merge time without am/pm with am/pm string

Parameters
Name Type Description
$date string
$format string

User time format

$mer string
Returns
Type Description
string
Details
Deprecated
for public use  
TODO
find better way to do this!  
methodpublicnow( ) : string

Return current datetime in local format

Returns
Type Description
string
methodpublicnowDate( ) : string

Return current date in User format

Returns
Type Description
string
methodpublicnowDb( ) : string

Return current time in DB format

Returns
Type Description
string
methodpublicnowDbDate( ) : string

Return current date in DB format

Returns
Type Description
string
methodpublicparseDateRange( string $range, \User $user = null, bool $adjustForTimezone = true ) : array

Parse date range expression Returns beginning and end of the range as a date

Parameters
Name Type Description
$range string
$user \User
$adjustForTimezone bool

Do we need to adjust for timezone?

Returns
Type Description
array of two Date objects, start & end
methodpublicsetAlwaysDb( bool $flag = true ) : \TimeDate

Set flag specifying we should always use DB format

Parameters
Name Type Description
$flag bool
Returns
Type Description
\TimeDate
methodpublicsetNow( \DateTime $now ) : \TimeDate

Set 'now' time For testability - predictable time value

Parameters
Name Type Description
$now \DateTime
Returns
Type Description
\TimeDate $this
methodpublicsetUser( \User $user = null ) : \TimeDate

Set current user for this object

Parameters
Name Type Description
$user \User

User object, default is current user

Returns
Type Description
\TimeDate
methodpublicsplitTime( string $date, string $format ) : array

Splits 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

Parameters
Name Type Description
$date string
$format string
Returns
Type Description
array
methodpublicsplit_date_time( string $datetime ) : array

Split datetime string into date & time

Parameters
Name Type Description
$datetime string
Returns
Type Description
array
methodpublicswap_formats( string $date, string $from, string $to ) : string

Parameters
Name Type Description
$date string
$from string
$to string
Returns
Type Description
string
Details
Deprecated
for public use Convert date from one format to another  
methodpublictimeSeparator( ) : string

Get user format's time separator

Returns
Type Description
string
methodpublictimeSeparatorFormat( string $timeformat ) : \stringS

Find out format's time separator

Parameters
Name Type Description
$timeformat string

Time format

Returns
Type Description
\stringS
methodpublicto_db( string $date ) : string

Convert date from local datetime to GMT-based DB datetime

Includes TZ conversion.

Parameters
Name Type Description
$date string
Returns
Type Description
string Datetime in DB format
methodpublicto_db_date( string $date, bool $convert_tz = true ) : string

Convert local datetime to DB date

TZ conversion depends on parameter. If false, only format conversion is performed.

Parameters
Name Type Description
$date string

Local date

$convert_tz bool

Should time and TZ be taken into account?

Returns
Type Description
string Date in DB format
methodpublicto_db_date_time( string $date, string $time ) : array

Takes a Date & Time value in local format and converts them to DB format No TZ conversion!

Parameters
Name Type Description
$date string
$time string
Returns
Type Description
array Date & time in DB format
methodpublicto_db_time( string $date, bool $convert_tz = true ) : string

Convert local datetime to DB time

TZ conversion depends on parameter. If false, only format conversion is performed.

Parameters
Name Type Description
$date string

Local date

$convert_tz bool

Should time and TZ be taken into account?

Returns
Type Description
string Time in DB format
methodpublicto_display( string $date, string $from, string $to ) : string

Convert date from format to format

No TZ conversion is performed!

Parameters
Name Type Description
$date string
$from string

Source format

$to string

Destination format

Returns
Type Description
string Converted date
methodpublicto_display_date( string $date, bool $convert_tz = true ) : string

Converts DB date string to local date string

TZ conversion depens on parameter

Parameters
Name Type Description
$date string

Date in DB format

$convert_tz bool

Perform TZ conversion?

Returns
Type Description
string Date in user-defined format
methodpublicto_display_date_time( string $date, bool $meridiem = true, bool $convert_tz = true, \User $user = null ) : string

Convert DB datetime to local datetime

TZ conversion is controlled by parameter

Parameters
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

Returns
Type Description
string Date in display format
methodpublicto_display_time( string $date, bool $meridiem = true, bool $convert_tz = true ) : string

Converts DB time string to local time string

TZ conversion depends on parameter

Parameters
Name Type Description
$date string

Time in DB format

$meridiem bool
$convert_tz bool

Perform TZ conversion?

Returns
Type Description
string Time in user-defined format
methodpublictzGMT( \DateTime $date ) : \DateTime

Convert DateTime to GMT timezone

Parameters
Name Type Description
$date \DateTime
Returns
Type Description
\DateTime
methodpublictzName( string | \DateTimeZone $name ) : string
static

Get display name for a certain timezone Note: it uses current date for GMT offset, so it may be not suitable for displaying generic dates

Parameters
Name Type Description
$name string | \DateTimeZone

TZ name

Returns
Type Description
string
methodpublictzUser( \DateTime $date, \User $user = null ) : \DateTime

Convert DateTime to user timezone

Parameters
Name Type Description
$date \DateTime
$user \User
Returns
Type Description
\DateTime
methodpublicuserTimezone( \User $user = null ) : string
static

Get the name of the timezone for the user

Parameters
Name Type Description
$user \User

User, default - current user

Returns
Type Description
string
methodpublicuserTimezoneSuffix( \DateTime $date, \User $user = null ) : string
static

Get the description of the user timezone for specific date Like: PST(+08:00) We need the date because it can be DST or non-DST Note it's different from TZ name in tzName() that relates to current date

Parameters
Name Type Description
$date \DateTime

Current date

$user \User

User, default - current user

Returns
Type Description
string
Documentation was generated by DocBlox 0.18.1.