Date

Handling date information

Returns object, which is extended from PHP DateTime -class. Adds methods for setting, manipulating and getting day information. As default, class will return date in local format.

Arguments

$date
Date in various string formats:
Y-m-d
d.m.Y
timestamp
date -String
eDate
Default: today
null, false Undefined date

Example

Get expected delivery date:

$DeliveryDate = $this->get('date', $order_date)->addDays(3)->setNextWorkday();

Get date

Current date

$this->get('date');

Specific date

You can give target date as a second parameter. Date can be in many formats: The given value is interpreted accordingly.

Y-m-d        $this->get('date','2013-04-13');
d.m.Y        $this->get('date','13.04.2013');
Timestamp    $this->get('date',1365870932);
Date -string $this->get('date','Tomorrow');
Date range -string $this->get('date','This week'); --> Monday
Date range -string $this->get('date','This week','end'); --> Sunday

Or if you know the correct format, you can use the explicit method to set the date:

Y,m,d       $this->get('date')->setDate('2013','04','13');
Y-m-d       $this->get('date')->setDateFromYmd('2013-04-13');
d.m.Y       $this->get('date')->setDateFromDmy('13.04.2013');
Timestamp   $this->get('date')->setDateFromTimestamp(1365870932);
Date string $this->get('date')->setDateFromString('Tomorrow');
Week        $this->get('date')->setWeek('15','2013');

Get date of value in date -field

You can create new eDate -object of date -fields with Field::getDate() -method.

$expirationDate = $Order->billing_date->getDate()->addDays(14);

You can get reference to eDate -object of value in Date -field using Field::getDateReference() method.

Add 14 days to value in field:

$Order->billing_date->getDateReference()->addDays(14);

All undefined method calls in Date -field are bridged to Field::getDateReference():

$Order->billing_date->addDays(14);

Modify date

You can create a date and then change its value with modifier -methods:

Tomorrow is             $this->get('date')->addDay();
Expiration date is      $this->get('date',$billingDate)->addWeeks(2);
Expiration date is      $this->get('date',$billingDate)->modify('+14 days');
This day in january is  $this->get('date')->setMonth(1);
First day for month     $this->get('date')->getFirstDateOfMonth(1);

Compare dates

Comparing functions creates a date from given parameter and compares it to current date value. Default target date is Today.

if ( $this->get('date',$date)->isToday() ){
    // Date is today
}
if ( $this->get('date',$date)->isAfter() ){
    // Date is after today
}
if ( $this->get('date',$date)->isAfterOr('Tomorrow') ){
    // Date is tomorrow or after
}
if ( $this->get('date',$date)->isBefore('2013-07-12') ){
    // Date is before 2013-07-12
}
if ( $this->get('date',$date)->isBefore('next friday') ){
    // Date is before next friday
}
if ( $this->get('date',$date)->isSame($dateParam,'week') ){
    // Date is in the same week as date in param
}
if ( $this->get('date',$date)->isAfter('Today','month') ){
    // Date is in next month or after
}
if ( $this->get('date',$date)->isBetween('yesterday','tomorrow') ){
    // Date is yesterday, today or tomorrow
}
if ( $this->get('date',$date)->isBetween($start,$end) ){
    // Date is: $start, $end, Between or start/end is not defined.
}

Iterate through dates

Go through all days in week

foreach ( $this->get('date',$date)->getDatesInWeek() as $Date ){
   $txt .= "Date: {$Date}";
}

Get all dates until given date:

foreach ( $this->get('date',$startDate)->getDatesTo($endDate) as $Date ){
   $txt .= "Date: {$Date}";
}

Loop through months (jan to dec):

foreach ( $this->get('date')->getMonths() as $Date ){
    $txt .= "{$Date->getMonth()}. month is {$Date->getMonthName()}";
}

Full method list

Date -class is chainable. ie. all methods that returns 'this' can be written in chain. When cast to string, class returns the date in local or setFormat() -format.

Setting date

Method Return type Description
setDate(Y,m,d) this Sets date from Y,m,d vars.
clearDate()  this  Setd date as 'undefined'.
setDateFromParam(mixed) this Sets date from unknown format. Same as create -parameter.
setDateFromYmd('Y-m-d') this Sets date from Y-m-d -format.
setYmd('Y-m-d') this Alias for setDateFromYmd().
setDateFromDmy('d.m.Y') this Sets date from d.m.Y -format.
setDateFromTimestamp( int ) this Sets date from UNIX timestamp.
setDateFromYmdhis( int ) this Sets date from YYYYMMDDHHIISS -format.
setDateFromString(string) this Sets date according to Date formats
setDateFromObject(object) this Sets date from eDate or DateTime -objects.

Modifying date

Method Return type Description
setYear(int) this Changes the year to explicit value.
setQuarter(int) this Changes the quarter to explicit value.
setMonth(int) this Changes the month to explicit value. Checks the number of days. ie. 31.01.2013 -> 28.2.2013
setWeek(vk[,year,day]) this Set given week/year/weekday.
setDay(int) this Changes the day to explicit value. Does noe change year/month (ie. day over month max days is set as month max days)
setDayOfWeek(int) this Changes the day to goven weekday 1=monday.
addYear() this Add one year.
addMonth() this Add one month. Checks the number of days. ie. 31.01.2013 -> 28.2.2013
addWeek() this Add one week.
addDay() this Add one day.
addYears(int) this Add one or more years.
addMonths(int) this Add one or more months. Checks the number of days. ie. 31.01.2013 -> 28.2.2013
addWeeks(int) this Add one or more weeks.
addDays(int) this Add one or more days.
modify(string) this Modifies date according to Relative date formats

Getting date information

Method Return type Example Description
format([format]) string 12.03.2021 Return date accoding to given Date format. If not set, uses local format.
getDateShort() string 12.03. Return date in short form in local format.
getTimestamp() int Date (and time) in UNIX timestamp.
getDaystamp() int Date (no time) in UNIX timestamp.
getYmd() string 2015-10-25 Date in Y-m-d -format.
getYear() int 2015 Year in four digits
getQuarter() int 4 Quarter: 1-4
getMonth() int 12 Month number: 1-12
getMonthName()  string January Locale name of the month.
getMonthNameShort()  string Jan Month name abbreviation.
getMonthYearName() string January 2015 Monthname and year
getWeek() int 53 Week number
getWeekYear() int 2015 Year of the week (31.12.2018 = 2019 vk 1)
getWeekYearName() string 2015 vk 25 Year and week.
getDay() int 31 Day of month.
getDayName()  string Monday Day name.
getDayNameShort() string Mo Day name abbreviation.
getDaysInMonth() int 28 Number of days in month. 28-31
getDaysInYear() int 365-366 Number of days in year.
isLeapYear() bool false True, if date is in leap -year.
getDayOfWeek() int 7 Day of week. 1=Monday
getDayOfMonth() int 31 Day of month: 1-31
getDayOfYear() int 365 Day of year: 1-366
getDate()  eDate   New eDate object with the value of original eDate.
isValid() bool  true Return true, if object has a valid date in it.
isNull() bool  false Return true, if object does not have valid date in it.

Workdays

Method Return type Description
isWorkday() bool True, if date is not holiday nor saturday.
getWorkdatesTo($date) eDate[] Date -objects of workdays in given range.
countWorkdaysTo($date) int Number of workdays in given range.
getWorkdatesInMonth() eDate[] Date -objects of workdays in given range
countWorkdaysInMonth() int Number of workdays in month.
setPrevWorkday() this Set previous working date, if date is weekend or extra holiday.
setNextWorkday() this Set next working date, if date is weekend or extra holiday.
setClosestWorkday()  this  If saturday, set last friday. If sunday, set next monday;
setClosestWorkdayInSameMonth()  this  Set last friday/next monday so that new day is in same month than the value.

Anniversary

Method Return type Description
getAnniversary()  eDate Anniversary date in current year.
getBirthday()  eDate Alias for getAnniversary()
getNextAnniversary()  eDate  If anniversary is in past for this year, get next year.
getNextBirthday()  eDate  Alias for getNextAnniversary()
getAge([$targetDate])  int  Age in targetDate (default: today).

Set/Get methods

eDate setter/getter -pattern: If given argument, it is set as value. If argument is not set, value is get.

Method Setter Getter
ymd([string]) setDateFromYmd() getYmd() 
year([int]) setYear(int) getYear()
quarter([int]) setQuarter(int) getQuarter()
month([int]) setMonth(int) getMonth()
week([int]) setWeek(int) getWeek() 
day([int]) setDay(int) getDay()

Date info

Method Return type Description
isPast() bool TRUE, if date is before current date.
isToday() bool TRUE, if date is today.
isFuture() bool TRUE, if date is after current date.
isMonday() bool TRUE, if date is monday.
isTuesday() bool TRUE, if date is tuesday.
isWednesday() bool TRUE, if date is wednesday.
isThursday() bool TRUE, if date is thursday.
isFriday() bool TRUE, if date is friday.
isSaturday() bool TRUE, if date is saturday.
isSunday() bool TRUE, if date is sunday.
isWeekend() bool TRUE, if date is saturday or sunday.
isHoliday()  bool  TRUE, if date is sunday or a special holiday (Finnish cal)
Is first/last Return type Description
isFirstDateOfWeek() bool TRUE, is date is first date of week
isFirstDateOfMonth() bool TRUE, is date is first date of month
isFirstDateOfQuarter() bool TRUE, is date is first date of quarter
isFirstDateOfYear() bool TRUE, is date is first date of year
isLastDateOfWeek() bool TRUE, is date is last date of week
isLastDateOfMonth() bool TRUE, is date is last date of month
isLastDateOfQuarter() bool TRUE, is date is last date of quarter
isLastDateOfYear() bool TRUE, is date is last date of year

Comparing dates

Compare to other date Return type Description
isSame(date,[unit]) bool TRUE, if date is same as given date.
isSameWeek(date) bool TRUE, if is in same week as given date.
isSameMonth(date) bool TRUE, if is in same month as given date.
isSameQuarter(date) bool TRUE, if is in same quarter as given date.
isSameYear(date) bool TRUE, if is in same year as given date.
isBefore(date,[unit]) bool TRUE, if date is before given date.
isBeforeOr(date,[unit]) bool TRUE, if date is before or the given date.
isAfter(date,[unit]) bool TRUE, if date is after given date.
isAfterOr(date,[unit]) bool TRUE, if date is after or the given date.
isBetween(date,date,[unit]) bool TRUE, if date is between or a given date.
isEffective(date,date,[unit]) bool TRUE, if date is effective in given date range: isBetween or start/end not defined.
getDaysTo([date]) int Day difference to target date: $Today->getDaysTo($Tomorrow) = 1
getDaysFrom([date]) int Day difference from target date: $Today->getDaysFrom($Tomorrow) = -1
getDaysAgo([date]) string Get day diff in human form: Tomorrow, Over 2 weeks from now...
diffDays(date)  int Get number of days between two days. $Today->diffDays($Tomorrow) = 1
countDaysTo(date) int Get number of days to given date. Alias to diffDays()
countDaysFrom(date) int Get number of days from given date. Alias to diffDays() * -1
diffMonths(date)  int Get number of months between two days. (Huomioi vain vuoden ja kuukauden, ei päivää)
countMonthsTo(date) int Get number of months to given date. Alias to diffMonths()
countMonthsFrom(date) int Get number of months from given date. Alias to diffMonths() * -1
getWeeksTo(date) int Get first date of weeks to given date.
getMonthsTo(date) int Get first date of months to given date.
getQuartersTo(date) int Get first date of quarters to given date.
getYearsTo(date) int Get first date of years to given date.
date
Comparison date in any eDate create -format.
unit
Comparison unit: day, week, month, quarter, year
Default: day
Comparison value Return type Description
getComparisonValue($unit) int Comparison value in given unit.
getDayValue() int yyyymmdd Comparison value in accuracy of day.
getWeekValue() int yyyyww Comparison value in accuracy of week.
getMonthValue() int yyyymm Comparison value in accuracy of month.
getQuarterValue() int yyyyq Comparison value in accuracy of quarter.
getYearValue() int yyyy Comparison value in accuracy of year.

Date periods

Method Return type Description
getFirstDateOfYear() eDate  The first date of year.
getLastDateOfYear() eDate  The last date of year.
getFirstDateOfQuarter() eDate  The first date of quarter.
getLastDateOfQuarter() eDate  The last date of quarter.
getFirstDateOfMonth() eDate  The first date of month.
getLastDateOfMonth() eDate  The last date of month.
getFirstDateOfWeek() eDate  The first date of week.
getLastDateOfWeek() eDate  The last date of week.
getDatesInWeek() eDate[]  Days of the week.
getDatesInMonth() eDate[]  Days of the month.
getDatesInYear() eDate[]  Days of the year.
getDatesTo($day) eDate[]  Days from the date to given day.

Misc

Method Return type Description
getMonths() eDate[]  Loop through months in current year. (first days of month)
::getHolidayList(start,end)  array Return the list of special holidays in given range: array[YYYY-MM-DD] = Name

Config

Method Return type Description
setFormat(string) this Sets return format
getFormat()  string Format that has been set to class.