Skip to content
Thiago Delgado Pinto edited this page Oct 22, 2016 · 6 revisions

#TDateTime

There are three available classes: TDateTime, TDate, and TTime. The first one inherits from PHP's \DateTime and it is the parent class of TDate and TTime. Thus, TDateTime is compatible with DateTime and can replace it. Its child classes just change the formatting methods, such as toString() and toDatabaseFormat() in order to facilitate the conversion to string or their storage into databases.

Main methods

The following methods are the main methods in TDateTime. Please check the class for all available methods.

Year

  • int year() gets the year;
  • TDateTime& setYear( int $year ) sets the year. This method is chainnable;
  • bool isLeapYear() returns true/false if the year is a leap year;
  • TDateTime& addYears( int $years ) adds years to the current date. This method is chainnable;
  • TDateTime& addOneYear() adds one year to the current date. This method is chainnable;
  • TDateTime& plusYears( int $years ) same as addYears();
  • TDateTime& plusOneYear() same as addOneYear();
  • TDateTime& subYears( int $years ) subtracts years from the current date. This method is chainnable;
  • TDateTime& subOneYear() subtracts one year from the current date. This method is chainnable;
  • TDateTime& minusYears( int $years ) same as subYears();
  • TDateTime& minusOneYear() same as subOneYear();

Month

  • int month() gets the month;
  • void setMonth( int $month ) sets the month. This method is chainnable;
  • int daysInMonth() returns the number of the days in the date's month;
  • TDateTime& addMonths( int $months ) adds $month times 30 days. This method is chainnable;
  • TDateTime& addOneMonth() adds 30 days. This method is chainnable;
  • TDateTime& plusMonths( int $months ) same as addMonths();
  • TDateTime& plusOneMonth() same as plusMonths();
  • TDateTime& subMonths( int $months ) substracts $month times 30 days. This method is chainnable;
  • TDateTime& subOneMonth() subtracts 30 days. This method is chainnable;
  • TDateTime& minusMonths( int $months ) same as subMonths();
  • TDateTime& minusOneMonth() same as subOneMonth();

Day

  • int day() returns the day (int);
  • TDateTime& setDay( int $day ) sets the day. This method is chainnable;
  • int dayOfYear() returns the day of the year (int, from 1 to 356);
  • int dayOfWeek() returns the day of the week (int, from 0 to 6 where 0 is Sunday);
  • int weekOfYear() returns the week of the year. According to ISO-8601, weeks start on Monday;
  • double age( \DateTime $currentDate = null ) returns the age of a person. By default, it considers today as the current date. You can specify the current date for the calculus;
  • bool isSunday() returns true if the date is a sunday;
  • bool isSaturday() returns true if the date is a saturday;
  • bool isWeekend() returns true if the date is a sunday or a saturday;
  • TDateTime& addDays( $days ) adds the given number of days. This method is chainnable;
  • TDateTime& addOneDay() adds one day. This method is chainnable;
  • TDateTime& plusDays( $days ) same as addDays();
  • TDateTime& plusOneDay() same as addOneDay();
  • TDateTime& subDays( $days ) subtracts the given number of days. This method is chainnable;
  • TDateTime& subOneDay() subtracts one day. This method is chainnable;
  • TDateTime& minusDays( $days ) same as subDays();
  • TDateTime& minusOneDay() same as subOneDay();

Hour

  • int hour() gets the hour;
  • TDateTime& setHour( int $hour ) sets the hour. This method is chainnable;
  • bool isAm() returns true if the hour is ante meridiem;
  • bool isPm() returns true if the hour is post meridiem;
  • bool isDaylightSavingTime() returns true if the time is in daylight saving;
  • int utcOffset() returns the UTF offset considering day light saving;
  • string timezoneName() returns the timezone name;
  • TDateTime& addHours( int $hours ) adds the given number of hours. This method is chainnable;
  • TDateTime& addOneHour() adds one hours. This method is chainnable;
  • TDateTime& plusHours( int $hours ) same as addHours();
  • TDateTime& plusOneHour() same as addOneHour();
  • TDateTime& subHours( int $hours ) subtracts the given number of hours. This method is chainnable;
  • TDateTime& subOneHour() subtracts one hours. This method is chainnable;
  • TDateTime& minusHours( int $hours ) same as subHours();
  • TDateTime& minusOneHour() same as subOneHour();

Minute

  • int minute() gets the minute;
  • TDateTime& setMinute( int $minute ) sets the minute. This method is chainnable;
  • TDateTime& addMinutes( int $minutes ) adds the given minutes. This method is chainnable;
  • TDateTime& addOneMinute() adds one minute. This method is chainnable;
  • TDateTime& plusMinutes( int $minutes ) same as addMinutes();
  • TDateTime& plusOneMinute() same as addOneMinute();
  • TDateTime& subMinutes( int $minutes ) subtracts the given minutes. This method is chainnable;
  • TDateTime& subOneMinute() subtracts one minute. This method is chainnable;
  • TDateTime& minusMinutes( int $minutes ) same as subMinute();
  • TDateTime& minusOneMinute() same as subOneMinute();

Second

  • int second() gets the second;
  • TDateTime& setSecond( int $second ) sets the second. This method is chainnable;
  • TDateTime& addSeconds( int $seconds ) adds the given seconds. This method is chainnable;
  • TDateTime& addOneSecond() adds one second. This method is chainnable;
  • TDateTime& plusSeconds( int $seconds ) same as addSeconds;
  • TDateTime& plusOneSecond() same as addOneSecond;
  • TDateTime& subSeconds( int $seconds ) subtracts the given seconds. This method is chainnable;
  • TDateTime& subOneSecond() subtracts one second. This method is chainnable;
  • TDateTime& minusSeconds( int $seconds ) same as subSeconds;
  • TDateTime& minusOneSecond() same as subOneSecond;

Micro

  • int micro() gets the microtime;

Diff

  • int|double diffInYears( \DateTime $dateTime, $absolute = true ) returns the difference in years from a given \DateTime. If absolute is true, it returns an integer value. Otherwise a double value;
  • int|double diffInMonths( \DateTime $dateTime, $absolute = true ) returns the difference in months from a given \DateTime. If absolute is true, it returns an integer value. Otherwise a double value;
  • int|double diffInDays( \DateTime $dateTime, $absolute = true ) returns the difference in days from a given \DateTime. If absolute is true, it returns an integer value. Otherwise a double value;
  • int|double diffInWeeks( \DateTime $dateTime, $absolute = true ) returns the difference in weeks from a given \DateTime. If absolute is true, it returns an integer value. Otherwise a double value;
  • int|double diffInHours( \DateTime $dateTime, $absolute = true ) returns the difference in hours from a given \DateTime. If absolute is true, it returns an integer value. Otherwise a double value;
  • int|double diffInMinutes( \DateTime $dateTime, $absolute = true ) returns the difference in minutes from a given \DateTime. If absolute is true, it returns an integer value. Otherwise a double value;
  • int|double diffInSeconds( \DateTime $dateTime, $absolute = true ) returns the difference in seconds from a given \DateTime. If absolute is true, it returns an integer value. Otherwise a double value;

Value handling utilities

  • TDateTime& clearDate() clears the date part of the datetime;
  • TDateTime& clearTime() clears the time part of the datetime;
  • TDateTime& clear() clear the date and time;
  • TDateTime& copyDate( \DateTime $dateTime ) copy the date part of a \DateTime;
  • TDateTime& copyTime( \DateTime $dateTime ) copy the time part of a \DateTime;
  • TDateTime& copyDateTime( \DateTime $dateTime ) copy a \DateTime;
  • TDateTime& copy( \DateTime $dateTime ) same as copyDateTime();
  • TDateTime newCopy() creates a copy of the current instance;

Comparison

  • bool after( \DateTime $dateTime );
  • bool before( \DateTime $dateTime );
  • bool between( \DateTime $min, \DateTime $max );
  • bool greaterThan( \DateTime $dateTime );
  • bool greaterThanOrEqualTo( \DateTime $dateTime );
  • bool lessThan( \DateTime $dateTime );
  • bool lessThanOrEqualTo( \DateTime $dateTime );
  • bool equalTo( \DateTime $dateTime );
  • bool notEqualTo( \DateTime $dateTime );

Conversion

  • string toString();
  • string dateString();
  • string timeString();
  • string simpleTimeString();
  • TDateTime& fromString( string $text, \DateTimeZone $timezone = null );
  • TDateTime& fromDateString( string $text, \DateTimeZone $timezone = null );
  • TDateTime& fromTimeString( string $text, \DateTimeZone $timezone = null );
  • TDateTime& fromSimpleTimeString(string $text, \DateTimeZone $timezone = null );
  • TDateTime& fromDateTime( \DateTime $dateTime );
  • TDateTime& parse( string $format , $time, \DateTimeZone $timezone = null );

Format-specific conversions

  • string toDatabaseString();
  • string toDatabaseDateString();
  • string toDatabaseTimeString();
  • string toDatabaseSimpleTimeString();
  • TDateTime& fromDatabaseString( string $text, \DateTimeZone $timezone = null );
  • TDateTime& fromDatabaseDateString( string $text, \DateTimeZone $timezone = null );
  • TDateTime& fromDatabaseTimeString( string $text, \DateTimeZone $timezone = null );
  • string toBrazilianString();
  • string toBrazilianDateString();
  • string toBrazilianTimeString();
  • string toBrazilianSimpleTimeString();
  • TDateTime& fromBrazilianString( string $text, \DateTimeZone $timezone = null );
  • TDateTime& fromBrazilianDateString( string $text, \DateTimeZone $timezone = null );
  • TDateTime& fromBrazilianTimeString( string $text, \DateTimeZone $timezone = null );
  • TDateTime& fromBrazilianSimpleTimeString( string $text, \DateTimeZone $timezone = null );
  • string toAmericanString();
  • string toAmericanDateString();
  • string toAmericanTimeString();
  • string toAmericanSimpleTimeString();
  • TDateTime& fromAmericanString( string $text, \DateTimeZone $timezone = null );
  • TDateTime& fromAmericanDateString( string $text, \DateTimeZone $timezone = null );
  • TDateTime& fromAmericanTimeString( string $text, \DateTimeZone $timezone = null );
  • TDateTime& fromAmericanSimpleTimeString( string $text, \DateTimeZone $timezone = null );

Validation

  • bool isValidTime( string $time, string $separator = ':' );
  • bool isValidSimpleTime( string $time, string $separator = ':' );
  • bool isValidDatabaseDateTime( string $dateTime, string $dateSeparator = '/', string $timeSeparator = ':' );
  • bool isValidDatabaseDate( string $date, string $separator = '/' );
  • bool isValidAmericanDateTime( string $dateTime, string $dateSeparator = '/', string $timeSeparator = ':' );
  • bool isValidAmericanDate( string $date, string $separator = '/' );
  • bool isValidBrazilianDateTime( string $dateTime, string $dateSeparator = '/', string $timeSeparator = ':' );
  • bool isValidBrazilianDate( string $date, string $separator = '/' );

Useful constants

Days of week:

  • SUNDAY = 0;
  • MONDAY = 1;
  • TUESDAY = 2;
  • WEDNESDAY = 3;
  • THURSDAY = 4;
  • FRIDAY = 5;
  • SATURDAY = 6;

Conversions:

  • MONTHS_PER_YEAR = 12;
  • WEEKS_PER_MONTH = 4;
  • DAYS_PER_WEEK = 7;
  • SECONDS_PER_MINUTE = 60;
  • MINUTES_PER_HOUR = 60;

Formats:

  • DATABASE_DATETIME_FORMAT = 'Y-m-d H:i:s';
  • DATABASE_DATE_FORMAT = 'Y-m-d';
  • DATABASE_TIME_FORMAT = 'H:i:s';
  • DATABASE_SIMPLE_TIME_FORMAT = 'H:i';
  • AMERICAN_DATETIME_FORMAT = 'm/d/Y H:i:s';
  • AMERICAN_DATE_FORMAT = 'm/d/Y';
  • AMERICAN_TIME_FORMAT = 'H:i:s';
  • AMERICAN_SIMPLE_TIME_FORMAT = 'H:i';
  • BRAZILIAN_DATETIME_FORMAT = 'd/m/Y H:i:s';
  • BRAZILIAN_DATE_FORMAT = 'd/m/Y';
  • BRAZILIAN_TIME_FORMAT = 'H:i:s';
  • BRAZILIAN_SIMPLE_TIME_FORMAT = 'H:i';