We now have a youtube channel. Subscribe!

PHP | Date and Time Functions

PHP Date & Time Functions


Hello folks! welcome back to a new edition of our tutorial on PHP. In this tutorial guide, we are going to be studying about the PHP Date and Time Functions.

The PHP Date and Time functions lets you get date & time from the server where your PHP scripts are running. You can use these functions in formatting date and time in so many different ways.

Installation

There is no Installation needed to use these functions; they are part of PHP core.


Runtime Configuration

The behavior of these functions is affected by the settings in php.ini. These parameters are available in PHP version 5 and onwards.

Date and Time Configuration Options

The following below is the list of date/time configuration options in PHP -

NameDefaultDescriptionChangeable
date.default_latitude"31.7667"Specifies the default latitude.PHP_INI_ALL
date.default_longitude"35.2333"Specifies the default longitudePHP_INI_ALL
date.sunrise_zenith"90.83"Specifies the default sunrise zenithPHP_INI_ALL
date.sunset_zenith"90.83"Specifies the default sunset zenithPHP_INI_ALL
date.timezone""Specifies the default timezonePHP_INI_ALL

List of PHP Date and Time Functions

PHP - denotes the earliest version of PHP that have support for the functions -

Sr.NoFunction & DescriptionPHP
1checkdate()

Validates a Gregorian date

3
2date_create()

Returns new DateTime object

5
3date_date_set()

Sets the date

5
4date_default_timezone_get()

Returns the default time zone

5
5date_default_timezone_set()

Sets the default time zone

5
6date_format()

Returns date formatted according to given format

5
7date_isodate_set()

Sets the ISO date

5
8date_modify()

Alters the timestamp

5
9date_offset_get()

Returns the daylight saving time offset

5
10date_parse()

Returns associative array with detailed info about given date

5
11date_sun_info()

Returns an array with information about sunset/sunrise and twilight begin/end.

5
12date_sunrise()

Returns the time of sunrise for a given day / location

5
13date_sunset()

Returns the time of sunset for a given day / location

5
14date_time_set()

Sets the time

5
15date_timezone_get()

Return time zone relative to given DateTime

5
16date_timezone_set()

Sets the time zone for the DateTime object

5
17date()

Formats a local time/date

3
18getdate()

Returns an array that contains date and time information for a Unix timestamp

3
19gettimeofday()

Returns an array that contains current time information

3
20gmdate()

Formats a GMT/UTC date/time

3
21gmmktime()

Returns the Unix timestamp for a GMT date

3
22gmstrftime()

Formats a GMT/UTC time/date according to locale settings

3
23idate()

Formats a local time/date as integer

5
24localtime()

Returns an array that contains the time components of a Unix timestamp

4
25microtime()

Returns the microseconds for the current time

3
26mktime()

Returns the Unix timestamp for a date

3
27strftime()

Formats a local time/date according to locale settings

3
28strptime()

Parses a time/date generated with strftime()

5
29strtotime()

Parses an English textual date or time into a Unix timestamp

3
30time()

Returns the current time as a Unix timestamp

3
31timezone_abbreviations_list()

Returns associative array containing dst, offset and the timezone name

5
32timezone_identifiers_list()

Returns numerically index array with all timezone identifiers

5
33timezone_name_from_abbr()

Returns the timezone name from abbrevation

5
34timezone_name_get()

Returns the name of the timezone

5
35timezone_offset_get()

Returns the timezone offset from GMT

5
36timezone_open()

Returns new DateTimeZone object

5
37timezone_transitions_get()

Returns all transitions for the timezone

5
38date_add()

Adds an interval to a date.

5.3
39date_create_from_format()

Creates a date by parsing a timestring according to a specified format.

5.3
40date_diff()

Calculates and returns the difference between two dates.

5.3
41date_parse_from_format()

Returns information about the given date according to the specified format.

5.3
42date_parse()

Returns an array congaing info about the given date.

5.2
43date_sub()

Subtracts a time interval from a DateTime object.

5.3
44date_timestamp_get()

Returns the Unix timestamp

5.3
45date_timestamp_set()

Sets the date and time value as per the given timestamp.

5.3
46date_get_last_errors()

Returns warnings and errors while creating a DateTime object.

5.3
47date_interval_create_from_date_string()

Creates a date interval from a given string.

5
48date_interval_format()

Formats the given interval.

5.5
49date_create_immutable_from_format()

Parses a timestring based on specified format.

5.5
50date_create_immutable()

Creates and returns a DateTimeImmutable object.

5.5
51timezone_version_get()

Returns the version of the current timezonedb.



In our subsequent tutorials, we are going to be discussing about the above listed built-in PHP functions.

PHP Date & Time Constants

The following below is the list of PHP date and time constants -

Sr.NoConstant & Description
1

DATE_ATOM

Atom (example: 2005-08-15T16:13:03+0000)

2

DATE_COOKIE

HTTP Cookies (example: Sun, 14 Aug 2005 16:13:03 UTC)

3

DATE_ISO8601

ISO-8601 (example: 2005-08-14T16:13:03+0000)

4

DATE_RFC822

RFC 822 (example: Sun, 14 Aug 2005 16:13:03 UTC)

5

DATE_RFC850

RFC 850 (example: Sunday, 14-Aug-05 16:13:03 UTC)

6

DATE_RFC1036

RFC 1036 (example: Sunday, 14-Aug-05 16:13:03 UTC)

7

DATE_RFC1123RFC

RFC 1123 (example: Sun, 14 Aug 2005 16:13:03 UTC)

8

DATE_RFC2822

RFC 2822 (Sun, 14 Aug 2005 16:13:03 +0000)

9

DATE_RSS

RSS (Sun, 14 Aug 2005 16:13:03 UTC)

10

DATE_W3C

World Wide Web Consortium (example: 2005-08-14T16:13:03+0000)

11

SUNFUNCS_RET_TIMESTAMP

Timestamp ( Available in 5.1.2 )

12

SUNFUNCS_RET_STRING

Hours:minutes (example: 08:02) ( Available in 5.1.2 )

13

SUNFUNCS_RET_DOUBLE

Hours as floating point number (example 8.75)( Available in 5.1.2 )



Alright guys! This is where we are going to be rounding up for this tutorial post. In our next tutorial, we are going to be discussing about the PHP checkdate() Function.

Feel free to ask your questions where necessary and we will attend to them as soon as possible. If this tutorial was helpful to you, you can use the share button to share this tutorial.

Follow us on our various social media platforms to stay updated with our latest tutorials. You can also subscribe to our newsletter in order to get our tutorials delivered directly to your emails.

Thanks for reading and bye for now.

Post a Comment

Hello dear readers! Please kindly try your best to make sure your comments comply with our comment policy guidelines. You can visit our comment policy page to view these guidelines which are clearly stated. Thank you.
© 2023 ‧ WebDesignTutorialz. All rights reserved. Developed by Jago Desain