PHP
  • PHPF1 / Utils
  • docs
    • Dates
Powered by GitBook
On this page
  • today
  • todayAsString
  • yesterday
  • yesterdayAsString
  • tomorrow
  • tomorrowAsString
  • differenceInDays
  • differenceInDaysFromString
  • addDaysToDate
  • addMinutesToTime
  • getLastDayOfMonth
  • timestampToDateTime
  • dateToTimestamp
  • getTimeDifferenceInMinutes
  • getDayOneWeekFromNow

Was this helpful?

  1. docs

Dates

Previousdocs

Last updated 4 years ago

Was this helpful?

Date and time related utility functions.

today

Returns today's date as a DateTime object.

Syntax:

public static function Dates::today() : DateTime

Parameters: None

Returns: DateTime

Example:

$today = Dates::today();

todayAsString

Returns today's date as string. By default, it returns the date in ISO format. The output format can be changed by the $pattern parameter.

Syntax:

public static function todayAsString($pattern = 'Y-m-d') : string

Parameters: $pattern: How to format the output string. The default value is Y-m-d

Returns: string

Example:

$today = Dates::todayAsString();

yesterday

Syntax:

public static function yesterday() : DateTime

Parameters: None

Returns: DateTime

Example:

$yesterday = Dates::yesterday();

yesterdayAsString

Syntax:

public static function yesterdayAsString($pattern = 'Y-m-d')

Parameters: $pattern : How to format the output string. The default value is Y-m-d

Returns: string

Example:

$yesterday = Dates::yesterdayAsString();

tomorrow

Syntax:

public static function tomorrow() : DateTime

Parameters: None

Returns: DateTime

Example:

$tomorrow = Dates::tomorrow();

tomorrowAsString

Syntax:

public static function tomorrowAsString($pattern = 'Y-m-d')

Parameters: $pattern : How to format the output string. The default value is Y-m-d

Returns: string

Example:

$tomorrow = Dates::tomorrowAsString();

differenceInDays

Syntax:

public static function differenceInDays(DateTime $startDate, DateTime $endDate) : int

Parameters: $startDate : The first date

$endDate : The second date

Returns: int

Example:

$day1 = new DateTime('2020-10-17');
$day2 = new DateTime('2020-11-03');

$diff = Dates::differenceInDays($day1, $day2);

differenceInDaysFromString

Syntax:

public static function differenceInDaysFromString(string $startDate, string $endDate) : int

Parameters: $startDate : The first date as string

$endDate : The second date as string

Returns: int

Example:

$diff = Dates::differenceInDays('2020-10-17', '2020-11-03');

addDaysToDate

Syntax:

public static function addDaysToDate(string $originalDate, int $days) : DateTime

Parameters: $originalDate : The base date as string

$days : Number of days to add

Returns: DateTime

Example:

$newDate = Dates::addDaysToDate('2020-10-17', 17);

addMinutesToTime

Syntax:

public static function addMinutesToTime(string $originalTime, int $minutes) : DateTime

Parameters: $originalDate : The base time as string

$minutes : Number of minutes to add

Returns: DateTime

Example:

$newTime = Dates::addMinutesToTime('2020-10-17 13:28', 48);

getLastDayOfMonth

Syntax:

public static function getLastDayOfMonth(string $actualDate) : DateTime

Parameters: $actualDate : The actual date as string

Returns: DateTime

Example:

$lastDay = Dates::getLastDayOfMonth('2020-10-17');

timestampToDateTime

Syntax:

public static function timestampToDateTime(int $timestamp) : DateTime

Parameters: $timestamp : The Unix timestamp to convert

Returns: DateTime

Example:

$date = Dates::timestampToDateTime(1565740800);

dateToTimestamp

Syntax:

public static function dateToTimestamp(string $dateStr) : int

Parameters: $dateStr : The date in string format to convert

Returns: int

Example:

$unixTimestamp = Dates::dateToTimestamp('2019-08-14');

getTimeDifferenceInMinutes

Syntax:

public static function getTimeDifferenceInMinutes(string $startTime, string $endTime) : int

Parameters: $startTime : The start time in string format

$endTime : The end time in string format

Returns: int

Example:

$diff = Dates::getTimeDifferenceInMinutes('2020-10-13 12:24', '2020-10-13 18:36');

getDayOneWeekFromNow

Syntax:

public static function getDayOneWeekFromNow() : DateTime

Parameters: None

Returns: DateTime

Example:

$nextWeekDay = Dates::getDayOneWeekFromNow();

Returns yesterday's date as a DateTime object.

Returns yesterday's date as string. By default, it returns the date in ISO format. The output format can be changed by the $pattern parameter.

Returns tomorrow's date as a DateTime object.

Returns tomorrow's date as string. By default, it returns the date in ISO format. The output format can be changed by the $pattern parameter.

Returns the difference in days between two dates.

Returns the difference in days between two dates that are defined as strings.

Adds days to the original date and returns the new date as a DateTime object.

Adds minutes to the original time and returns the new time as a DateTime object.

Returns the last day of the month based on the specified date.

Converts the given Unix timestamp to a DateTime object.

Returns the Unix timestamp representation of a given date.

Gets the difference between two date times in minutes.

Returns the date one week from now.

Source
Source
Source
Source
Source
Source
Source
Source
Source
Source
Source
Source
Source
Source
Source