Skip to content

classes_base_datetimeex.class

Daniel Spors edited this page Dec 19, 2023 · 3 revisions

Classes in file base/datetimeex.class.php

class DateTimeEx

Extends DateTime with sime useful methods.

Extends: DateTime

Age

Calculates the age in x Depending on $unit returns the age of the object in years, months, days, hours, minutes or secods.

Definition: public function Age($unit, $zero_point=false)

Returns: float The calculated age

Parameters:

  • string $unit Values: sec, min, hour, day, weeks, month, year

  • DateTimeEx $zero_point The point in time this DateTimeEx object shall be compared to. Defaults to DateTimeEx::Now()

age_secs

SHORTCUT DateTimeEx::Age(DateTimeEx::SECONDS)

FirstDayOfMonth

Returns a new DateTimeEx object representing the first day of the month. If $date is given that date's first day of month will be returned.

Definition: public static function FirstDayOfMonth($date=false)

Returns: DateTimeEx The created instance

Parameters:

  • mixed $date Starting point for the calculation

FirstDayOfWeek

Returns a new DateTimeEx object representing the first day of the week. If $date is given that date's first day of week will be returned.

Definition: public static function FirstDayOfWeek($date=false)

Returns: DateTimeEx The created instance

Parameters:

  • mixed $date Starting point for the calculation

FirstDayOfYear

Returns a new DateTimeEx object representing the first day of the year. If $date is given that date's first day of year will be returned.

Definition: public static function FirstDayOfYear($date=false)

Returns: DateTimeEx The created instance

Parameters:

  • mixed $date Starting point for the calculation

formatIso8601

SHORTCUT DateTimeEx('Y-m-d\TH:i:s.v').'Z'

FromOtherTZ

Returns a new DateTimeEx from datamatching another Timzone.

Definition: public static function FromOtherTZ($source, $other_timezone, $format=false)

Returns: DateTimeEx

Parameters:

  • mixed $source DateTimeEx::Make compatible creation data

  • string $other_timezone The timezone $source is from

  • string $format Optional Format, see DateTimeEx::Make

getTimestampMs

Returns the milliseconds since 1970-01-01 00:00:00.

Definition: public function getTimestampMs()

Returns: int

is_future_date

Checks if this represents a date/time in the future. That sounds mystic, in fact just return $this > self::Now();

Definition: public function is_future_date(bool $date_only=false)

Returns: bool true or false

Parameters:

  • bool $date_only If true compared only the date part (default: false)

is_past_date

Checks if this represents a date/time in the past. In fact just return $this < self::Now();

Definition: public function is_past_date(bool $date_only=false)

Returns: bool true or false

Parameters:

  • bool $date_only If true compared only the date part (default: false)

is_today

Checks if this DateTimeEx is today.

Definition: public function is_today()

Returns: bool True if today, else false

Make

Creates a new DateTimeEx object ready for method chaining.

Definition: public static function Make($source=false, $format=false)

Returns: DateTimeEx The created instance

Parameters:

  • mixed $source DateTimeEx, DateTime or anything DateTime accepts in it's constructor

  • string $format Optional format to use when parsing $source

midnight

Returns the midnight value.

Definition: public function midnight()

Returns: DateTimeEx $this

Now

Returns a new DateTimeEx object representing 'now'.

Definition: public static function Now()

Returns: DateTimeEx The created instance

Offset

Adds an offset. Will not modify this object but return a clone. $value and $interval are in fact only separated for good readability:

$dte->Offset(1,'day')->Offset('1 day',''); // all the same	
$dte->Offset('1 day ','+ 1 year')->Offset(1,'day + 1 year'); // all the same	

Definition: public function Offset($value, $interval)

Returns: DateTimeEx A new instance

Parameters:

olderThan

Checks if this object is older than x Best to understand with some samples:

$dte->olderThan(1,'day');	
$dte->olderThan(30,'seconds');	
$dte->olderThan(15,'months');	

Definition: public function olderThan($value, $interval)

Returns: bool true or false

Parameters:

  • int $value Offset value

  • string $interval Unit

ot_days

SHORTCUT DateTimeEx::olderThan($days,DateTimeEx::DAYS)

ot_hours

SHORTCUT DateTimeEx::olderThan($days,DateTimeEx::HOURS)

ot_mins

SHORTCUT DateTimeEx::olderThan($days,DateTimeEx::MINUTES)

Today

Returns a new DateTimeEx object representing the current day at midnight.

Definition: public static function Today()

Returns: DateTimeEx The created instance

ToOtherTZ

Returns this as seem in another timezone.

Definition: public function ToOtherTZ($other_timezone)

Returns: DateTimeEx

Parameters:

  • string $other_timezone Target timzone

youngerThan

Checks if this object is younger than x Best to understand with some samples:

$dte->youngerThan(1,'day');	
$dte->youngerThan(30,'seconds');	
$dte->youngerThan(15,'months');	

Definition: public function youngerThan($value, $interval)

Returns: bool true or false

Parameters:

  • int $value Offset value

  • string $interval Unit

yt_days

SHORTCUT DateTimeEx::youngerThan($days,DateTimeEx::DAYS)

yt_hours

SHORTCUT DateTimeEx::youngerThan($days,DateTimeEx::HOURS)

yt_mins

SHORTCUT DateTimeEx::youngerThan($days,DateTimeEx::MINUTES)

Clone this wiki locally