Curriculum
Course: PHP Basic
Login

Curriculum

PHP Basic

PHP Install

0/1

PHP Casting

0/1

PHP Constants

0/1

PHP Magic Constants

0/1

PHP Operators

0/1

PHP Reference

0/276
Text lesson

PHP Date

PHP Date/Time Introduction

The date/time functions in PHP let you retrieve the date and time from the server where your script runs and format it in various ways.

Note: These functions rely on your server’s locale settings. Be sure to account for daylight saving time and leap years when using them.

Installation

The PHP date/time functions are built into the PHP core, so no additional installation is needed to use them.

Runtime Configuration

The behavior of these functions is influenced by settings in the php.ini configuration file.

Name

Description

Default

PHP Version

date.timezone

The default timezone used by all date/time functions

“”

PHP 5.1

date.default_latitude

The default latitude used by the date_sunrise() and date_sunset() functions

 “31.7667”

PHP 5.0

date.default_longitude

The default longitude used by the date_sunrise() and date_sunset() functions

“35.2333”

PHP 5.0

date.sunrise_zenith

The default sunrise zenith used by the date_sunrise() and date_sunset() functions

“90.83”

PHP 5.0

date.sunset_zenith

The default sunset zenith used by the date_sunrise() and date_sunset() functions

“90.83”

PHP 5.0

 

PHP Date/Time Functions

 

Function

Description

checkdate()

Validates a date in the Gregorian calendar.

date_add()

Adds days, months, years, hours, minutes, and seconds to a date.

date_create_from_format()

Returns a new DateTime object formatted according to a specified format.

date_create()

Returns a new DateTime instance

date_date_set()

Sets a new date.

date_default_timezone_get()

Returns the default timezone used by all date/time functions.

date_default_timezone_set()

Sets the default timezone for all date/time functions.

date_diff()

Returns the interval between two dates.

date_format()

Returns a date formatted according to a specified pattern.

date_get_last_errors()

Returns any warnings or errors found in a date string.

date_interval_create_from_date_string()

Creates a DateInterval from the components specified in a string.

date_interval_format()

Formats the interval.

date_isodate_set()

Sets the date in ISO format.

date_modify()

Adjusts the timestamp.

date_offset_get()

Returns the timezone offset.

date_parse_from_format()

Returns an associative array with detailed information about a specified date, formatted according to a given pattern.

date_parse()

Returns an associative array with detailed information about a specified date.

date_sub()

Subtracts days, months, years, hours, minutes, and seconds from a date.

date_sun_info()

Returns an array with information about sunrise, sunset, and twilight start/end times for a specified day and location.

date_sunrise()

Returns the sunset time for a specified date and location.

date_sunset()

Returns the sunset time for a specified date and location.

date_time_set()

Sets the time.

date_timestamp_get()

Returns a Unix timestamp.

date_timestamp_set()

Sets the date and time using a Unix timestamp.

date_timezone_get()

Returns the time zone of the specified DateTime object.

date_timezone_set()

Sets the time zone for the DateTime object.

date()

Formats a local date and time.

getdate()

Returns date and time information for a given timestamp or the current local date and time.

gettimeofday()

Returns the current time.

gmdate()

Formats a GMT/UTC date and time.

gmmktime()

Returns the Unix timestamp for a GMT date.

gmstrftime()

Formats a GMT/UTC date and time based on locale settings.

idate()

Formats a local time/date as an integer.

localtime()

Returns the local time.

microtime()

Returns the current Unix timestamp including microseconds.

mktime()

Returns the Unix timestamp for a specified date.

strftime()

Formats a local time and/or date based on locale settings.

strptime()

Parses a time/date string generated with strftime().

strtotime()

Converts an English textual datetime description into a Unix timestamp.

time()

Returns the current time as a Unix timestamp.

timezone_abbreviations_list()

Returns an associative array containing dst, offset, and the time zone name.

timezone_identifiers_list()

Returns an indexed array of all timezone identifiers.

timezone_location_get()

Returns location information for a specified time zone.

timezone_name_from_ abbr()

Returns the timezone name corresponding to a given abbreviation.

timezone_name_get()

Returns the name of the timezone.

timezone_offset_get()

Returns the timezone offset from GMT.

timezone_open()

Creates a new DateTimeZone object.

timezone_transitions_get()

Returns all transitions for the timezone.

timezone_version_get()

Returns the version of the timezone database.

PHP Predefined Date/Time Constants

Constant

Description

DATE_ATOM

Atom (example: 2019-01-18T14:13:03+00:00)

DATE_COOKIE

HTTP Cookies (example: Fri, 18 Jan 2019 14:13:03 UTC)

DATE_ISO8601

ISO-8601 (example: 2019-01-18T14:13:03+0000)

DATE_RFC822

RFC 822 (example: Fri, 18 Jan 2019 14:13:03 +0000)

DATE_RFC850

RFC 850 (example: Friday, 18-Jan-19 14:13:03 UTC)

DATE_RFC1036

RFC 1036 (example: Friday, 18-Jan-19 14:13:03 +0000)

DATE_RFC1123

RFC 1123 (example: Fri, 18 Jan 2019 14:13:03 +0000)

DATE_RFC2822

RFC 2822 (example: Fri, 18 Jan 2019 14:13:03 +0000)

DATE_RFC3339

Same as DATE_ATOM (since PHP 5.1.3)

DATE_RFC3339_EXTENDED

RFC3339 Extended format (since PHP 7.0.0) (example: 2019-01-18T16:34:01.000+00:00)

DATE_RSS

RSS (Fri, 18 Jan 2019 14:13:03 +0000)

DATE_W3C

World Wide Web Consortium (example: 2019-01-18T14:13:03+00:00)

SUNFUNCS_RET_TIMESTAMP

Timestamp (since PHP 5.1.2)

SUNFUNCS_RET_STRING

Hours:minutes (example: 09:41) (since PHP 5.1.2)

SUNFUNCS_RET_DOUBLE

Hours as a floating point number (example: 9.75) (since PHP 5.1.2)