Oihana PHP

date

Table of Contents

Functions

formatDateTime()  : string
Formats a given date/time string into a specified format and timezone.
isDate()  : bool
Indicates if the passed-in expression is a valid date with a specific format (default 'Y-m-d').
isValidTimezone()  : bool
Indicates if the passed-in expression is a valid timezone.
now()  : string
Returns the current date/time as a formatted string.

Functions

formatDateTime()

Formats a given date/time string into a specified format and timezone.

formatDateTime([string|null $date = null ][, string $timezone = 'UTC' ][, string|null $format = 'Y-m-dTH:i:s.vZ' ]) : string

This function attempts to validate and parse the input date string. If the input date is not valid according to the provided format, it defaults to the current date/time ("now"). It then applies the specified timezone and returns the formatted date string.

Parameters
$date : string|null = null

The input date/time string to format. If null or invalid, "now" is used.

$timezone : string = 'UTC'

The timezone identifier (e.g., 'Europe/Paris'). Defaults to 'UTC'.

$format : string|null = 'Y-m-dTH:i:s.vZ'

The date format string compatible with DateTime::format(). Defaults to 'Y-m-d\TH:i:s.v\Z' (ISO 8601 UTC with milliseconds).

Tags
throws
DateInvalidTimeZoneException

If the provided timezone string is invalid.

throws
DateMalformedStringException

If the input date string is malformed or cannot be parsed.

example
echo formatDateTime('2025-07-20 15:30', 'Europe/Paris', 'Y-m-d H:i');
// Output: '2025-07-20 15:30'

echo formatDateTime();
// Output: current date/time in UTC, e.g., '2025-07-20T13:30:20.676Z'
author

Marc Alcaraz (ekameleon)

since
1.0.0
Return values
string

The formatted date/time string, or null if creation fails.

isDate()

Indicates if the passed-in expression is a valid date with a specific format (default 'Y-m-d').

isDate(string|null $date[, string $format = 'Y-m-d' ]) : bool
Parameters
$date : string|null

The expression to evaluate.

$format : string = 'Y-m-d'

The date format (default 'Y-m-d').

Tags
example
var_dump(isDate('2025-07-20'));           // true
var_dump(isDate('20/07/2025', 'd/m/Y'));  // true
var_dump(isDate('invalid-date'));         // false
var_dump(isDate(null));                   // false
author

Marc Alcaraz (ekameleon)

since
1.0.0
Return values
bool

Indicates if the passed-in expression is a valid timezone.

isValidTimezone()

Indicates if the passed-in expression is a valid timezone.

isValidTimezone([string|null $timezone = null ]) : bool

Note: timezone_identifiers_list() requires PHP >= 5.2

Parameters
$timezone : string|null = null

The timezone expression to evaluates.

Tags
example
var_dump(isValidTimezone('Europe/Paris')); // true
var_dump(isValidTimezone('Invalid/Timezone')); // false
var_dum(isValidTimezone(null)); // false
author

Marc Alcaraz (ekameleon)

since
1.0.0
Return values
bool

Indicates if the passed-in expression is a valid timezone.

now()

Returns the current date/time as a formatted string.

now([string $timezone = 'UTC' ][, string|null $format = 'Y-m-dTH:i:s.vZ' ]) : string
Parameters
$timezone : string = 'UTC'

The timezone identifier (e.g., 'Europe/Paris'). Defaults to 'UTC'.

$format : string|null = 'Y-m-dTH:i:s.vZ'

The date format string compatible with DateTime::format(). Defaults to 'Y-m-d\TH:i:s.v\Z' (ISO 8601 UTC with milliseconds).

Tags
throws
DateInvalidTimeZoneException

If the provided timezone string is invalid.

throws
DateMalformedStringException

If the date creation fails (should not occur with 'now').

example
echo now() ;
// Output: '2025-07-20T15:30.676Z'
author

Marc Alcaraz (ekameleon)

since
1.0.0
Return values
string

The formatted current date/time string.


        
On this page

Search results