Sometimes it is necessary to display the current time and date in your input field of the form, or any other part of the web page.PHP provides inbuilt date and time functions that can be used to display time and date from the server. PHP also provides different functions for formatting the structure of date and time.
Index
PHP Date/Time Functions
PHP provides the following functions for formatting the date and time according to the requirements:
1] The checkdate() function
This function is used for checking if the entered date is valid or not.
Syntax:
checkdate(month ,day ,year)
Here all the fields are required in checkdate function.
Example:
<?php var_dump(checkdate(2,1,-400)); echo "<br>"; var_dump(checkdate(17,20,2019)); echo "<br>"; var_dump(checkdate(2,20,2017)); ?>
Output:
bool(false)
bool(false)
bool(true)
2] The date_add() function
This function adds some date, month, or time in the date.
Syntax:
date_add(object , interval)
Here all the fields are required.
Example:
<?php $date=date_create("2019-10-10"); date_add($date,date_interval_create_from_date_string("20 days")); echo date_format($date,"Y-m-d"); ?>
Output:
2019-10-30
3] The date_create_from_format() function
This function returns the new DateTime object according to the entered format.
Syntax:
date_create_from_format(format, time, timezone)
Here format and time are required, but timezone is optional.
Example:
<?php $date=date_create_from_format("j-M-Y","15-Mar-2019"); echo date_format($date,"Y/m/d"); ?>
Output:
2019/03/15
4] The date_create() function
This function returns the new DateTime object.
Syntax:
date_creat(time, timezone)
Here both fields are optional.
Example:
<?php $date=date_create("2019-03-15"); echo date_format($date,"Y/m/d"); ?>
Output:
2019/03/15
5] The date_date_set() function
This function sets a new date.
Syntax:
date_date_set(object, year, month, day)
Here all fields are required.
Example:
<?php $date=date_create(); date_date_set($date,2020,10,10); echo date_format($date,"Y/m/d"); ?>
Output:
2020/10/10
6] The date_default_timezone_get() function
This function returns the default timezone used by all the functions in the same script.
Syntax:
date_default_timezone_get()
Example:
<?php echo date_default_timezone_get(); ?>
Output:
UTC
7] The date_default_timezone_set() function
This function sets the default timezone used by all the functions in the same script.
Syntax:
date_default_timezone_set()
Example:
<?php date_default_timezone_set("Asia/Bangkok"); echo date_default_timezone_get(); ?>
Output:
Asia/Bangkok
8] The date_diff() function
This function returns the difference between the date and time objects.
Syntax:
date_diff(datetime1, datetime2, absolute)
Here the first two fields are required, and the third field is optional.
Example:
<?php $date1=date_create("2019-01-02"); $date2=date_create("2019-10-10"); $diff=date_diff($date1,$date2); echo $diff->format("%R%a days"); ?>
Output:
+281 days
9] The date_format() function
This function returns the formate of the date according to the format type.
Syntax:
date_format(object, format)
Here both fields are required.
Example:
<?php $date=date_create("2019-10-10"); echo date_format($date,"Y/m/d H:i:s"); ?>
Output:
2019/10/10 00:00:00
10] The date_get_last_errors() function
This function returns the errores while parsing the date string .
Syntax:
date_get_last_errors()
Example:
<?php date_create("gyuhkjedyui%&&/"); print_r(date_get_last_errors()); ?>
Output:
Array ( [warning_count] => 1 [warnings] => Array ( [6] => Double timezone specification ) [error_count] => 5 [errors] => Array ( [0] => The timezone could not be found [11] => Unexpected character [12] => Unexpected character [13] => Unexpected character [14] => Unexpected character ) )
11] The date_interval_create_from_date_string() function
This function sets the date interval from relative part of the string.
Syntax:
date_interval_create_from_date_string(datetime)
Here the field is required.
Example:
<?php $date = date_create('2019-10-10'); date_add($date, date_interval_create_from_date_string('1 year 20 days')); echo date_format($date, 'Y-m-d'); ?>
Output:
2020-10-30
12] The date_interval_format() function
This functionis an alias of DateInterval::format().
Syntax:
DateInterval::format(format)
Here the field is required.
Example:
<?php $date1=date_create("2019-02-01"); $date2=date_create("2019-10-10"); $diff=date_diff($date1,$date2); // the total number of days echo $diff->format("Total count of days: %a."); echo "<br>"; // outputs + beacause $date2 is after $date1 (a positive interval) echo $diff->format("Total count of days: %R%a."); echo "<br>"; // the number of days that is not already covered by the month echo $diff->format("Month: %m, days: %d."); ?>
Output:
Total number of days: 251.
Total number of days: +251.
Month: 8, days: 9.
13] The date_isodate_set() function
This function sets the date according to ISO 8601.
Syntax:
date_isodate_set(object, year, week, day)
Here other than a day, all the field is required.
Example:
<?php $date=date_create(); date_isodate_set($date,2019,2); echo date_format($date,"Y-m-d") . "<br>"; ?>
Output:
2019-01-07
14] The date_modify() function
This function modifies the timestamp.
Syntax:
date_modify(object, modify)
Here all the fields are required.
Example:
<?php $date=date_create("2019-10-10"); date_modify($date,"+15 days"); echo date_format($date,"Y-m-d"); ?>
Output:
2019-10-25
15] The date_offset_get() function
This function returns the timezone offset.
Syntax:
date_offset_get(object)
Here the field is required.
Example:
<?php $winter=date_create("2019-10-31",timezone_open("Europe/Oslo")); $summer=date_create("2019-01-30",timezone_open("Europe/Oslo")); echo date_offset_get($winter) . " seconds.<br>"; echo date_offset_get($summer) . " seconds."; ?>
Output:
3600 seconds.
3600 seconds.
16] The date_parse_from_format() function
This function returns an associative array with detailed information about a specified date, according to the specified format.
Syntax:
date_parse_from_format(format, date)
Here all the fields are required.
Example:
<?php print_r(date_parse_from_format("mmddyyyy","10102019")); echo "<br><br>"; print_r(date_parse_from_format("j.n.Y H:iP","12.7.2019 14:35+02:00")); ?>
Output:
Array ( [year] => [month] => 10 [day] => 19 [hour] => [minute] => [second] => [fraction] => [warning_count] => 0 [warnings] => Array ( ) [error_count] => 1 [errors] => Array ( [8] => Data missing ) [is_localtime] => )
Array ( [year] => 2019 [month] => 7 [day] => 12 [hour] => 14 [minute] => 35 [second] => 0 [fraction] => [warning_count] => 0 [warnings] => Array ( ) [error_count] => 0 [errors] => Array ( ) [is_localtime] => 1 [zone_type] => 1 [zone] => 7200 [is_dst] => )
17] The date_parse() function
This function returns the array with detailed information about the date.
Syntax:
date_parse(date)
Here the field is required.
Example:
<?php print_r(date_parse("2019-10-10 12:30:45.5")); ?>
Output:
Array ( [year] => 2019 [month] => 10 [day] => 10 [hour] => 12 [minute] => 30 [second] => 45 [fraction] => 0.5 [warning_count] => 0 [warnings] => Array ( ) [error_count] => 0 [errors] => Array ( ) [is_localtime] => )
18] The date_sub() function
This function subtracts some day, month, year, hour , minute or second from the date.
Syntax:
date_sub(object, interval)
Here both the field is required.
Example:
<?php $date=date_create("2019-10-10"); date_sub($date,date_interval_create_from_date_string("5 days")); echo date_format($date,"Y-m-d"); ?>
Output:
2019-10-05
19] The date_sun_info() function
This function returns the array containing the information about sunrise and sunset for a specific day and location.
Syntax:
date_sun_info(timestamp, location, longitude)
Here all the fields are required.
Example:
<?php $sun_info=date_sun_info(strtotime("2019-01-02"),31.7667,35.2333); foreach ($sun_info as $key=>$val) { echo "$key: " . date("H:i:s",$val) . "<br>"; } ?>
Output:
sunrise: 04:39:08
sunset: 14:46:43
transit: 09:42:56
civil_twilight_begin: 04:12:26
civil_twilight_end: 15:13:26
nautical_twilight_begin: 03:42:05
nautical_twilight_end: 15:43:46
astronomical_twilight_begin: 03:12:24
astronomical_twilight_end: 16:13:27
20] The date_sunrise() function
This function returns the information about sunrise for a specific day and location.
Syntax:
date_sunrise(timestamp, format, latitude, longitude, zenith, gmtoffset)
Here timestamp is only required.
Example:
<?php echo("Date: " . date("D M d Y")); echo("<br>Sunrise time: "); echo(date_sunrise(time(),SUNFUNCS_RET_STRING,48.4,-6,90,1)); ?>
Output:
Date: Fri Nov 08 2019
Sunrise time: 08:24
21] The date_sunset() function
This function returns the information about sunset for a specific day and location.
Syntax:
date_sunset(timestamp, format, latitude, longitude, zenith, gmtoffset)
Here timestamp is only required.
Example:
<?php echo("Date: " . date("D M d Y")); echo("<br>Sunset time: "); echo(date_sunset(time(),SUNFUNCS_RET_STRING,48.4,-6,90,1)); ?>
Output:
Date: Fri Nov 08 2019
Sunset time: 17:51
22] The date_time_set() function
This function sets the time.
Syntax:
date_time_set(object, hour, minute, second, microseconds)
Here object, hour, and minute are required.
Example:
<?php $date=date_create("2019-10-10"); date_time_set($date,13,33); echo date_format($date,"Y-m-d H:i:s") . "<br>"; date_time_set($date,6,20,28); echo date_format($date,"Y-m-d H:i:s"); ?>
Output:
2019-10-10 13:33:00
2019-10-10 06:20:28
23] The date_timestamp_get() function
This function returns the Unix timestamp.
Syntax:
date_timestamp_get(object)
Here the object is required.
Example:
<?php $date=date_create(); echo date_timestamp_get($date); ?>
Output:
1573182759
24] The date_timestamp_set() function
This function sets the date and time based on the Unix timestamp.
Syntax:
date_timestamp_set(object, unixtimestamp)
Here both the fields are required.
Example:
<?php $date=date_create(); date_timestamp_set($date,1971903391); echo date_format($date,"U = Y-m-d H:i:s"); ?>
Output:
1971903391 = 2032-06-26 22:56:31
25] The date_timezone_get() function
This function returns the timezone of given Datetime object.
Syntax:
date_timezone_get(object)
Here the object field is required.
Example:
<?php $date=date_create(null,timezone_open("Asia/Calcutta")); $tz=date_timezone_get($date); echo timezone_name_get($tz); ?>
Output:
Asia/Calcutta
27] The date_timezone_set() function
This function sets the timezone of given DateTime object.
Syntax:
date_timezone_set(object, timezone)
Here both fields are required.
Example:
<?php $date=date_create("2013-05-25",timezone_open("Asia/Calcutta")); echo date_format($date,"Y-m-d H:i:sP") . "<br>"; date_timezone_set($date,timezone_open("Europe/Paris")); echo date_format($date,"Y-m-d H:i:sP"); ?>
Output:
2013-05-25 00:00:00+05:30
2013-05-24 20:30:00+02:00
28] The date() function
This function formats the date and time and returns the formatted date string.
Syntax:
date(format, timestamp)
Here format field is required.
Example:
<?php // Prints the day echo date("l") . "<br>"; // Prints the day, date, month, year, time, AM or PM echo date("l jS \of F Y h:i:s A") . "<br>"; ?>
Output:
Friday
Friday 8th of November 2019 03:28:22 AM
29] The getdate() function
This function returns the date and time of a timestamp.
Syntax:
getdate(timestamp)
Here timestamp field is optional.
Example:
<?php // Print the array from getdate() print_r(getdate()); ?>
Output:
Array ( [seconds] => 7 [minutes] => 32 [hours] => 3 [mday] => 8 [wday] => 5 [mon] => 11 [year] => 2019 [yday] => 311 [weekday] => Friday [month] => November [0] => 1573183927 )
30] The gettimeofday() function
This function returns the current time.
Syntax:
gettimeofday(return_float)
Here return_float field is optional.
Example:
<?php print_r(gettimeofday()); ?>
Output:
Array ( [sec] => 1573184204 [usec] => 260818 [minuteswest] => 0 [dsttime] => 0 )
31] The gmdate() function
This function formats the GMT/UTC date and time and returns the date string.
Syntax:
gmdate(format, timestamp)
Here timestamp field is optional.
Example:
<?php // Prints the day echo gmdate("l") . "<br>"; // Prints the day, date, month, year, time, AM or PM echo gmdate("l jS \of F Y h:i:s A") . "<br>"; ?>
Output:
Friday
Friday 8th of November 2019 03:41:17 AM
32] The gmmktime() function
This function returns the unix timestamp for GMT date.
Syntax:
gmmktime(hour, minute, second, month, day, year, is_dst)
Here all fields are optional.
Example:
<?php echo "Jan 2, 2000 was on a ".date("l", gmmktime(0,0,0,1,2,2000)); ?>
Output:
Jan 2, 2000, was on a Sunday
33] The gmstrftime() function
This function formats the GMT/UTC and date according to the local setting.
Syntax:
gmstrftime(format, timestamp)
Here format field is optional.
Example:
<?php echo(gmstrftime("%B %d %Y, %X %Z",mktime(8,29,33,10,10,19))."<br>"); setlocale(LC_ALL,"hu_HU.UTF8"); echo(gmstrftime("%Y. %B %d. %A. %X %Z")); ?>
Output:
October 10 2019, 08:29:33 GMT
2019. November 08. Friday. 03:47:50 GMT
34] The idate() function
This function formats the local time and dates integer.
Syntax:
idate(format, timestamp)
Here timestamp field is optional.
Example:
<?php echo idate("B") . "<br>"; echo idate("d") . "<br>"; echo idate("h") . "<br>"; echo idate("H") . "<br>"; echo idate("i") . "<br>"; echo idate("I") . "<br>"; echo idate("L") . "<br>"; echo idate("m") . "<br>"; echo idate("s") . "<br>"; echo idate("t") . "<br>"; echo idate("U") . "<br>"; echo idate("w") . "<br>"; echo idate("W") . "<br>"; echo idate("y") . "<br>"; echo idate("Y") . "<br>"; echo idate("z") . "<br>"; echo idate("Z") . "<br>"; ?>
Output:
202
8
3
3
51
0
0
11
11
30
1573185071
5
45
19
2019
311
0
35] The localtime() function
This function returns the local time.
Syntax:
localtime(timestamp, is_assoc)
Here both fields are optional.
Example:
<?php print_r(localtime()); echo "<br><br>"; print_r(localtime(time(),true)); ?>
Output:
Array ( [0] => 12 [1] => 54 [2] => 3 [3] => 8 [4] => 10 [5] => 119 [6] => 5 [7] => 311 [8] => 0 )
Array ( [tm_sec] => 12 [tm_min] => 54 [tm_hour] => 3 [tm_mday] => 8 [tm_mon] => 10 [tm_year] => 119 [tm_wday] => 5 [tm_yday] => 311 [tm_isdst] => 0 )
36] The microtime() function
The microtime() function returns the current Unix timestamp with the accuracy of microseconds.
Syntax:
microtime(return_float)
Here return_float field is optional.
Example:
<?php echo(microtime()); ?>
Output:
0.45871400 1573185458
37] The mktime() function
This function returns the UNIX timestamp for a date .
Syntax:
mktime(hour, minute, second, month, day, year, is_dst)
Here all fields are optional.
Example:
<?php echo "Jan 2, 2000 was on a ".date("l", mktime(0,0,0,2,1,2000)) . "<br>"; ?>
Output:
Jan 2, 2000, was on a Tuesday
38] The strftime() function
This function formats the local time or date according to the local setting.
Syntax:
strftime(format, timestamp)
Here timestamp field is optional.
Example:
<?php echo(strftime("%B %d %Y, %X %Z",mktime(20,0,0,10,10,19))."<br>"); setlocale(LC_ALL,"hu_HU.UTF8"); echo(strftime("%Y. %B %d. %A. %X %Z")); ?>
Output:
October 10 2019, 20:00:00 UTC
2019. November 08. Friday. 06:03:08 UTC
39] The strptime() function
This function parses the time and date.
Syntax:
strptime(date, format)
Here all fields are required.
Example:
<?php $format="%d/%m/%Y %H:%M:%S"; $strf=strftime($format); echo("$strf"); print_r(strptime($strf,$format)); ?>
Output:
08/11/2019 06:08:33Array ( [tm_sec] => 33 [tm_min] => 8 [tm_hour] => 6 [tm_mday] => 8 [tm_mon] => 10 [tm_year] => 119 [tm_wday] => 5 [tm_yday] => 311 [unparsed] => )
40] The strtotime() function
This function parses an English textual DateTime into a Unix timestamp.
Syntax:
strtotime(time, now)
Here time field is required.
Example:
<?php echo(strtotime("now") . "<br>"); echo(strtotime("10 October 2019") . "<br>"); echo(strtotime("+5 hours") . "<br>"); echo(strtotime("+1 week") . "<br>"); echo(strtotime("next Monday") . "<br>"); echo(strtotime("last Sunday")); ?>
Output:
1573193501
1570665600
1573211501
1573798301
1573430400
1572739200
41] The time() function
This function returns the current time in the number of seconds.
Syntax:
time()
Example:
<?php $t=time(); echo($t . "<br>"); echo(date("Y-m-d",$t)); ?>
Output:
1573193688
2019-11-08
42] The timezone_abbreviations_list() function
This function returns an associative array, including DST, offset, and the timezone name.
Syntax:
timezone_abbreviations_list()
Example:
<?php $tzlist = DateTimeZone::listAbbreviations(); print_r($tzlist["acst"]); ?>
Output:
Array ( [0] => Array ( [dst] => [offset] => 32400 [timezone_id] => Australia/Adelaide ) [1] => Array ( [dst] => [offset] => 34200 [timezone_id] => Australia/Adelaide ) [2] => Array ( [dst] => [offset] => 32400 [timezone_id] => Australia/Broken_Hill ) [3] => Array ( [dst] => [offset] => 32400 [timezone_id] => Australia/Darwin ) [4] => Array ( [dst] => [offset] => 32400 [timezone_id] => Australia/North ) [5] => Array ( [dst] => [offset] => 32400 [timezone_id] => Australia/South ) [6] => Array ( [dst] => [offset] => 32400 [timezone_id] => Australia/Yancowinna ) [7] => Array ( [dst] => [offset] => 34200 [timezone_id] => Australia/Broken_Hill ) [8] => Array ( [dst] => [offset] => 34200 [timezone_id] => Australia/Darwin ) [9] => Array ( [dst] => [offset] => 34200 [timezone_id] => Australia/North ) [10] => Array ( [dst] => [offset] => 34200 [timezone_id] => Australia/South ) [11] => Array ( [dst] => [offset] => 34200 [timezone_id] => Australia/Yancowinna ) )
43] timezone_identifiers_list() function
It returns an indexed array containing all timezone identifiers.
Syntax:
timezone_identifiers_list(what, country)
Here both the fields are optional.
Example:
<?php // Print all timezones in Africa print_r(timezone_identifiers_list(1)); // Print the whole list //print_r(timezone_identifiers_list()); ?>
Output:
Array ( [0] => Africa/Abidjan [1] => Africa/Accra [2] => Africa/Addis_Ababa [3] => Africa/Algiers [4] => Africa/Asmara [5] => Africa/Bamako [6] => Africa/Bangui [7] => Africa/Banjul [8] => Africa/Bissau [9] => Africa/Blantyre [10] => Africa/Brazzaville [11] => Africa/Bujumbura [12] => Africa/Cairo [13] => Africa/Casablanca [14] => Africa/Ceuta [15] => Africa/Conakry [16] => Africa/Dakar [17] => Africa/Dar_es_Salaam [18] => Africa/Djibouti [19] => Africa/Douala [20] => Africa/El_Aaiun [21] => Africa/Freetown [22] => Africa/Gaborone [23] => Africa/Harare [24] => Africa/Johannesburg [25] => Africa/Juba [26] => Africa/Kampala [27] => Africa/Khartoum [28] => Africa/Kigali [29] => Africa/Kinshasa [30] => Africa/Lagos [31] => Africa/Libreville [32] => Africa/Lome [33] => Africa/Luanda [34] => Africa/Lubumbashi [35] => Africa/Lusaka [36] => Africa/Malabo [37] => Africa/Maputo [38] => Africa/Maseru [39] => Africa/Mbabane [40] => Africa/Mogadishu [41] => Africa/Monrovia [42] => Africa/Nairobi [43] => Africa/Ndjamena [44] => Africa/Niamey [45] => Africa/Nouakchott [46] => Africa/Ouagadougou [47] => Africa/Porto-Novo [48] => Africa/Sao_Tome [49] => Africa/Tripoli [50] => Africa/Tunis [51] => Africa/Windhoek )
44] timezone_location_get() function
It returns the location information for a given timezone.
Syntax:
timezone_location_get(object)
Here object field is required.
Example:
<?php $tz=timezone_open("Asia/Calcutta"); print_r(timezone_location_get($tz)); ?>
Output:
Array ( [country_code] => ?? [latitude] => 0 [longitude] => 0 [comments] => ? )
45] timezone_open() function
It creates the new DatetimeZone object.
Syntax:
timezone_open(timezone)
Here timezone field is required.
Example:
<?php $tz=timezone_open("Asia/Calcutta"); echo timezone_name_get($tz); ?>
Output:
Asia/Calcutta
Conclusion
These are the different types of date and time functions, which are useful in PHP programming. Users can use the results as per the requirements.
I hope you found this post informative.
Thanks for reading 🙂