revision:
Syntax: date(format, timestamp)
parameters:
format: required; specifies the format of the timestamp.
timestamp: optional; specifies a timestamp. Default is the current date and time.
d - represents the day of the month (01 to 31);
m - represents a month (01 to 12);
Y - represents a year (in four digits);
l (lowercase 'L') - represents the day of the week.
Other characters - like "/", ".", or "-" - can also be inserted between the characters to add additional formatting.
examples:
<?php echo "Today is " . date("Y/m/d") . "<br>"; echo "Today is " . date("Y.m.d") . "<br>"; echo "Today is " . date("Y-m-d") . "<br>"; echo "Today is " . date("l") . "<br>"; ?> © 2010-<?php echo date("Y");?>
H - 24-hour format of an hour (00 to 23);
h - 12-hour format of an hour with leading zeros (01 to 12);
i - minutes with leading zeros (00 to 59);
s - seconds with leading zeros (00 to 59);
a - lowercase "Ante meridiem" and "Post meridiem" (am or pm)
examples:
<?php echo "The time is " . date("h:i:sa"); ?>
If the time you got back from the code is not correct, it's probably because your server is in another country or set up for a different timezone. If you need the time to be correct according to a specific location, you can set the timezone you want to use. E.g. date_default_timezone_set("America/New_York");
examples:
<?php date_default_timezone_set("America/New_York"); echo "The time is " . date("h:i:sa")."<br><br>"; ?> <?php date_default_timezone_set("Asia/Shanghai"); echo "The time is " . date("h:i:sa"); ?>
This function returns the Unix timestamp for a date. The Unix timestamp contains the number of seconds between the Unix Epoch (January 1 1970 00:00:00 GMT) and the time specified.
Syntax: mktime(hour, minute, second, month, day, year)
examples:
<?php $d=mktime(11, 14, 54, 8, 12, 2014); echo "Created date is " . date("Y-m-d h:i:sa", $d); ?>
This function is used to convert a human readable date string into a Unix timestamp (the number of seconds since January 1 1970 00:00:00 GMT).
Syntax: strtotime(time, now)
examples:
<?php $d=strtotime("10:30pm April 15 2014"); echo "Created date is " . date("Y-m-d h:i:sa", $d); echo "<br><br>"; ?> <?php $d=strtotime("tomorrow"); echo date("Y-m-d h:i:sa", $d) . "<br>"; echo "<br>"; $d=strtotime("next Saturday"); echo date("Y-m-d h:i:sa", $d) . "<br>"; echo "<br>"; $d=strtotime("+3 Months"); echo date("Y-m-d h:i:sa", $d) . "<br>"; ?>
d - The day of the month (from 01 to 31)
D - A textual representation of a day (three letters)
j - The day of the month without leading zeros (1 to 31)
l (lowercase 'L') - A full textual representation of a day
N - The ISO-8601 numeric representation of a day (1 for Monday, 7 for Sunday)
S - The English ordinal suffix for the day of the month (2 characters st, nd, rd or th. Works well with j)
w - A numeric representation of the day (0 for Sunday, 6 for Saturday)
F - A full textual representation of a month (January through December)
m - A numeric representation of a month (from 01 to 12)
M - A short textual representation of a month (three letters)
n - A numeric representation of a month, without leading zeros (1 to 12)
t - The number of days in the given month
L - Whether it's a leap year (1 if it is a leap year, 0 otherwise)
o - The ISO-8601 year number
Y - A four digit representation of a year
y - A two digit representation of a year
a - Lowercase am or pm
A - Uppercase AM or PM
B - Swatch Internet time (000 to 999)
g - 12-hour format of an hour (1 to 12)
G - 24-hour format of an hour (0 to 23)
h - 12-hour format of an hour (01 to 12)
H - 24-hour format of an hour (00 to 23)
i - Minutes with leading zeros (00 to 59)
s - Seconds, with leading zeros (00 to 59)
u - Microseconds (added in PHP 5.2.2)
e - The timezone identifier (Examples: UTC, GMT, Atlantic/Azores)
I (capital i) - Whether the date is in daylights savings time (1 if Daylight Savings Time, 0 otherwise)
O - Difference to Greenwich time (GMT) in hours (Example: +0100)
P - Difference to Greenwich time (GMT) in hours:minutes (added in PHP 5.1.3)
T - Timezone abbreviations (Examples: EST, MDT)
Z - Timezone offset in seconds. The offset for timezones west of UTC is negative (-43200 to 50400)
c - The ISO-8601 date (e.g. 2013-05-05T16:34:42+00:00)
r - The RFC 2822 formatted date (e.g. Fri, 12 Apr 2013 12:01:05 +0200)
U - The seconds since the Unix Epoch (January 1 1970 00:00:00 GMT)
1 = AFRICA
2 = AMERICA
4 = ANTARCTICA
8 = ARCTIC
16 = ASIA
32 = ATLANTIC
64 = AUSTRALIA
128 = EUROPE
256 = INDIAN
512 = PACIFIC
1024 = UTC
2047 = ALL
4095 = ALL_WITH_BC
4096 = PER_COUNTRY
[tm_sec] - seconds
[tm_min] - minutes
[tm_hour] - hour
[tm_mday] - day of the month
[tm_mon] - month of the year (January=0)
[tm_year] - Years since 1900
[tm_wday] - Day of the week (Sunday=0)
[tm_yday] - Day of the year
[tm_isdst] - Is daylight savings time in effect
The date/time functions allow you to get the date and time from the server where your PHP script runs. You can then use the date/time functions to format the date and time in several ways. These functions depend on the locale settings of your server. Remember to take daylight saving time and leap years into consideration when working with these functions.
checkdate(month, day, year) - validates a Gregorian date;
examples:
<?php var_dump(checkdate(12,31,-400)); echo "<br>"; var_dump(checkdate(2,29,2003)); echo "<br>"; var_dump(checkdate(2,29,2004)); ?>
date_add(object, interval) - adds days, months, years, hours, minutes, and seconds to a date.
examples:
<?php $date=date_create("2013-03-15"); date_add($date,date_interval_create_from_date_string("40 days")); echo date_format($date,"Y-m-d"); ?>
date_create_from_format(format, time, timezone) - returns a new DateTime object formatted according to a specified format;
examples:
<?php $date=date_create_from_format("j-M-Y","15-Mar-2013"); echo date_format($date,"Y/m/d"); ?>
date_create(time, timezone) - returns a new DateTime object;
examples:
<?php $date=date_create("2013-03-15"); echo date_format($date,"Y/m/d"); echo "<br>"; $date=date_create("2013-03-15 23:40:00",timezone_open("Europe/Oslo")); echo date_format($date,"Y/m/d H:iP"); ?>
date_date_set(object, year, month, day) - sets a new date;
examples:
<?php $date=date_create(); date_date_set($date,2020,10,30); echo date_format($date,"Y/m/d"); ?>
date_default_timezone_get() - returns the default timezone used by all date/time functions;
examples:
<?php echo date_default_timezone_get(); ?>
date_default_timezone_set(timezone) - sets the default timezone used by all date/time functions;
examples:
<?php date_default_timezone_set("Asia/Bangkok"); echo date_default_timezone_get(); ?>
date_diff(datetime1, datetime2, absolute) - returns the difference between two dates;
examples:
<?php $date1=date_create("2013-03-15"); $date2=date_create("2013-12-12"); $diff=date_diff($date1,$date2); echo $diff->format("%R%a days"); ?>
date_format(object, format) - returns a date formatted according to a specified format;
examples:
<?php $date=date_create("2013-03-15"); echo date_format($date,"Y/m/d H:i:s"); ?>
date_get_last_errors() - returns the warnings/errors found in a date string;
examples:
<?php date_create("gyuiyiuyui%&&/"); print_r(date_get_last_errors()); ?>
date_interval_create_from_date_string(datetime) - sets up a DateInterval from the relative parts of the string;
examples:
<?php $date = date_create('2019-01-01'); date_add($date, date_interval_create_from_date_string('1 year 35 days')); echo date_format($date, 'Y-m-d'); ?>
date_interval_format(format) - formats the interval;
examples:
<?php $date1=date_create("2013-01-01"); $date2=date_create("2013-02-10"); $diff=date_diff($date1,$date2); // %a outputs the total number of days echo $diff->format("Total number of days: %a."); echo "<br>"; // %R outputs + beacause $date2 is after $date1 (a positive interval) echo $diff->format("Total number of days: %R%a."); echo "<br>"; // %d outputs the number of days that is not already covered by the month echo $diff->format("Month: %m, days: %d."); ?>
date_isodate_set(object, year, week, day) - sets the ISO date;
examples:
<?php $date=date_create(); date_isodate_set($date,2013,5); echo date_format($date,"Y-m-d") . "<br>"; date_isodate_set($date,2013,5,2); echo date_format($date,"Y-m-d"); ?>
date_modify(object, modify) - modifies the timestamp;
examples:
<?php $date=date_create("2013-05-01"); date_modify($date,"+15 days"); echo date_format($date,"Y-m-d"); ?>
date_offset_get(object) - returns the timezone offset;
examples:
<?php $winter=date_create("2013-12-31",timezone_open("Europe/Oslo")); $summer=date_create("2013-06-30",timezone_open("Europe/Oslo")); echo date_offset_get($winter) . " seconds.<br>"; echo date_offset_get($summer) . " seconds."; ?>
date_parse_from_format(format, date) - returns an associative array with detailed info about a specified date, according to a specified format;
examples:
<?php print_r(date_parse_from_format("mmddyyyy","05122013")); echo "<br><br>"; print_r(date_parse_from_format("j.n.Y H:iP","12.5.2013 14:35+02:00")); ?>
date_parse(date) - returns an associative array with detailed info about a specified date;
examples:
<?php print_r(date_parse("2013-05-01 12:30:45.5")); ?>
date_sub(object, interval) - subtracts days, months, years, hours, minutes, and seconds from a date;
examples:
<?php $date=date_create("2013-03-15"); date_sub($date,date_interval_create_from_date_string("40 days")); echo date_format($date,"Y-m-d"); ?>
date_sun_info(timestamp, latitude, longitude) - returns an array containing info about sunset/sunrise and twilight begin/end, for a specified day and location;
examples:
<?php echo "<h4>Date: 1st January, 2013</h4>"; $sun_info=date_sun_info(strtotime("2013-01-01"),31.7667,35.2333); foreach ($sun_info as $key=>$val) { echo "$key: " . date("H:i:s",$val) . "<br>"; } echo "<h4>Date: 1st June, 2013</h4>"; $sun_info=date_sun_info(strtotime("2013-06-01"),31.7667,35.2333); foreach ($sun_info as $key=>$val){ echo "$key: " . date("H:i:s",$val) . "<br>"; } ?>
date_sunrise(timestamp, format, latitude, longitude, zenith, gmtoffset) - returns the sunrise time for a specified day and location;
examples:
<?php //Calculate the sunrise time for Lisbon, Portugal //Latitude: 38.4 North //Longitude: 9 West //Zenith ~= 90 //offset: +1 GMT echo("<h4>Lisbon, Portugal</h4>"); echo("Date: " . date("D M d Y")); echo("<br>Sunrise time: "); echo(date_sunrise(time(),SUNFUNCS_RET_STRING,38.4,-9,90,1)); echo("<br>Sunset time: "); echo(date_sunset(time(),SUNFUNCS_RET_STRING,38.4,-9,90,1)); ?>
date_sunset(timestamp, format, latitude, longitude, zenith, gmtoffset) - returns the sunset time for a specified day and location;
examples:
<?php //Calculate the sunrise time for Lisbon, Portugal //Latitude: 38.4 North //Longitude: 9 West //Zenith ~= 90 //offset: +1 GMT echo("<h4>Lisbon, Portugal</h4>"); echo("Date: " . date("D M d Y")); echo("<br>Sunrise time: "); echo(date_sunrise(time(),SUNFUNCS_RET_STRING,38.4,-9,90,1)); echo("<br>Sunset time: "); echo(date_sunset(time(),SUNFUNCS_RET_STRING,38.4,-9,90,1)); ?>
date_time_set(object, hour, minute, second, microseconds) - sets the time;
examples:
<?php $date=date_create("2013-05-01"); date_time_set($date,13,24); echo date_format($date,"Y-m-d H:i:s") . "<br>"; date_time_set($date,12,20,55); echo date_format($date,"Y-m-d H:i:s"); ?>
date_timestamp_get(object) - returns the Unix timestamp;
examples:
<?php $date=date_create(); echo date_timestamp_get($date); ?>
date_timestamp_set(object, unixtimestamp) - Sets the date and time based on a Unix timestamp;
examples:
<?php $date=date_create(); date_timestamp_set($date,1371803321); echo date_format($date,"U = Y-m-d H:i:s"); ?>
date_timezone_get(object) - returns the time zone of the given DateTime object;
examples:
<?php $date=date_create(null,timezone_open("Europe/Paris")); $tz=date_timezone_get($date); echo timezone_name_get($tz); ?>
date_timezone_set(object, timezone) - sets the time zone for the DateTime object;
examples:
<?php $date=date_create("2013-05-25",timezone_open("Indian/Kerguelen")); 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"); ?>
date(format, timestamp) - formats a local date and time;
examples:
<?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>"; // Prints October 3, 1975 was on a Friday echo "Oct 3,1975 was on a ".date("l", mktime(0,0,0,10,3,1975)) . "<br>"; // Use a constant in the format parameter echo date(DATE_RFC822) . "<br>"; // prints something like: 1975-10-03T00:00:00+00:00 echo date(DATE_ATOM,mktime(0,0,0,10,3,1975)); ?>
getdate(timestamp) - returns date/time information of a timestamp or the current local date/time;
examples:
<?php // Print the array from getdate() print_r(getdate()); echo "<br><br>"; // Return date/time info of a timestamp; then format the output $mydate=getdate(date("U")); echo "$mydate[weekday], $mydate[month] $mydate[mday], $mydate[year]"; ?>
gettimeofday(return_float) - returns the current time;
examples:
<?php // Print the array from gettimeofday() print_r(gettimeofday()); echo "<br><br>"; // Print the float from gettimeofday() echo gettimeofday(true) . "<br><br>"; // Return current time; then format the output $mytime=gettimeofday(); echo "$mytime[sec].$mytime[usec]"; ?>
gmdate(format, timestamp) - formats a GMT/UTC date and time;
examples:
<?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>"; // Prints October 3, 1975 was on a Thursday echo "Oct 3, 1975 was on a ".gmdate("l", mktime(0,0,0,10,3,1975)) . "<br>"; // Use a constant in the format parameter echo gmdate(DATE_RFC822) . "<br>"; // prints something like: 1975-10-03T00:00:00+00:00 echo gmdate(DATE_ATOM,mktime(0,0,0,10,3,1975)); ?>
gmmktime(hour, minute, second, month, day, year, is_dst) - returns the Unix timestamp for a GMT date;
examples:
<?php // Prints: October 3, 1975 was on a Friday echo "Oct 3, 1975 was on a ".date("l", gmmktime(0,0,0,10,3,1975)); ?>
gmstrftime(format, timestamp) - formats a GMT/UTC date and time according to locale settings;
examples:
<?php echo(gmstrftime("%B %d %Y, %X %Z",mktime(20,0,0,12,31,98))."
"); setlocale(LC_ALL,"hu_HU.UTF8"); echo(gmstrftime("%Y. %B %d. %A. %X %Z")); ?>
idate(format, timestamp) - formats a local time/date as integer;
examples:
<?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>"; ?>
localtime(timestamp, is_assoc) - returns the local time;
examples:
<?php print_r(localtime()); echo "<br><br>"; print_r(localtime(time(),true)); ?>
microtime(return_float) - returns the current Unix timestamp with microseconds;
examples:
<?php echo(microtime()); /** * Simple function to replicate PHP 5 behaviour */ function microtime_float() { list($usec, $sec) = explode(" ", microtime()); return ((float)$usec + (float)$sec); } $time_start = microtime_float(); // Sleep for a while usleep(100); $time_end = microtime_float(); $time = $time_end - $time_start; echo "Did nothing in $time seconds\n"; ?>
mktime(hour, minute, second, month, day, year, is_dst) - returns the Unix timestamp for a date;
examples:
<?php // Prints: October 3, 1975 was on a Friday echo "Oct 3, 1975 was on a ".date("l", mktime(0,0,0,10,3,1975)) . "<br><br>"; //The mktime() function is useful for doing date arithmetic and validation. //It will automatically calculate the correct value for out-of-range input: echo date("M-d-Y",mktime(0,0,0,12,36,2001)) . "<br>"; echo date("M-d-Y",mktime(0,0,0,14,1,2001)) . "<br>"; echo date("M-d-Y",mktime(0,0,0,1,1,2001)) . "<br>"; echo date("M-d-Y",mktime(0,0,0,1,1,99)) . "<br>"; ?>
strftime(format, timestamp) - formats a local time and/or date according to locale settings;
examples:
<?php echo(strftime("%B %d %Y, %X %Z",mktime(20,0,0,12,31,98))."<br>"); setlocale(LC_ALL,"hu_HU.UTF8"); echo(strftime("%Y. %B %d. %A. %X %Z")); ?>
strptime(date, format) - parses a time/date generated with strftime();
Note: This function is not implemented on Windows platforms!
examples:
<?php $format="%d/%m/%Y %H:%M:%S"; $strf=strftime($format); echo("$strf"); print_r(strptime($strf,$format)); ?>
strtotime(time, now) - parses an English textual datetime into a Unix timestamp;
examples:
<?php echo(strtotime("now") . "<br>"); echo(strtotime("3 October 2005") . "<br>"); echo(strtotime("+5 hours") . "<br>"); echo(strtotime("+1 week") . "<br>"); echo(strtotime("+1 week 3 days 7 hours 5 seconds") . "<br>"); echo(strtotime("next Monday") . "<br>"); echo(strtotime("last Sunday")); ?>
time() - returns the current time as a Unix timestamp;
examples:
<?php $t=time(); echo($t . "<br>"); echo(date("Y-m-d",$t)); ?>
timezone_abbreviations_list() - returns an associative array containing dst, offset, and the timezone name;
examples:
<?php $tzlist = DateTimeZone::listAbbreviations(); print_r($tzlist["acst"]); ?>
timezone_identifiers_list(what, country) - returns an indexed array with all timezone identifiers;
examples:
<?php // Print all timezones in Africa print_r(timezone_identifiers_list(1)); // Print the whole list //print_r(timezone_identifiers_list()); ?>
timezone_location_get(object) - returns location information for a specified timezone;
examples:
<?php $tz=timezone_open("Asia/Taipei"); print_r(timezone_location_get($tz)); ?>
timezone_name_from_ abbr(abbr, gmtoffset, isdst) - returns the timezone name from abbreviation;
examples:
<?php echo timezone_name_from_abbr("EST") . "<br>"; echo timezone_name_from_abbr("",7200,0); ?>
timezone_name_get(object) - returns the name of the timezone;
examples:
<?php $tz=timezone_open("Europe/Paris"); echo timezone_name_get($tz); ?>
timezone_offset_get(object, datetime) - returns the timezone offset from GMT (in seconds);
examples:
<?php $tz=timezone_open("Asia/Taipei"); $dateTimeOslo=date_create("now",timezone_open("Europe/Oslo")); echo timezone_offset_get($tz,$dateTimeOslo); ?>
timezone_open(timnezone) - creates new DateTimeZone object;
examples:
<?php $tz=timezone_open("Europe/Paris"); echo timezone_name_get($tz); ?>
timezone_transitions_get(object, timestamp_start, timestamp_end) - returns all transitions for the timezone;
examples:
<?php $timezone = new DateTimeZone("Europe/Paris"); // Procedural style print_r(reset(timezone_transitions_get($timezone))); echo "<br><br>"; // Object oriented style print_r(reset($timezone->getTransitions())); ?>
timezone_version_get() - returns the version of the timezonedb;
examples:
<?php echo timezone_version_get(); ?>
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).