Google News
logo
PHP Date and Time Functions
The PHP date and time functions allow you to get the date and time from the server where your PHP scripts are running. You can use these functions to format the date and time in many different ways.

The date and time information is internally stored as a 64-bit number so all conceivably useful dates (including negative years) are supported. The range is from about 292 billion years in the past to the same in the future.

PHP date function is an in-built function that simplify working with date data types. The PHP date function is used to format a date or time into a human readable format. It can be used to display the date of article was published. 
The PHP Date() Function
The PHP date() function formats a timestamp to a more readable date and time.
Syntax
<?php
   date(format,[timestamp]);
?>

date(…) : This is the function that returns the current time on the server.

format : Is the general format which we want our output to be i.e.;

Y-m-d : The PHP date format YYYY-MM-DD

Y : Current year

m : Represents a month (01 to 12)

d : Represents the day of the month (01 to 31)

l (lowercase 'L') : Represents the day of the week

[timestamp] : This is optional. If no timestamp has been provided, PHP will get the use the php current date time on the server.

Other characters, like"/", ".", or "-" can also be inserted between the characters to add additional formatting.

Basic Example
<!DOCTYPE html>
<html>
<head>
    <title>PHP Date Basic Syntax</title>
</head>
<body>
<?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 />";
	echo "Today is  : " . date("D") . "<br />";
	echo "Today date is  : " . date("d") . "<br />";
	echo "This Month is : " . date("M") . "<br />";
	echo "This year is : " . date("Y");
?>  
</body>
</html>
Output :
Automatic Copyright Year
Use the date() function to automatically update the copyright year on your website:
<!DOCTYPE html>
<html>
<head>
    <title>PHP Automatic Copyright Year</title>
</head>
<body>

	<p>All &copy; rights reserved 2016 - <?php echo date("Y");?></p>

</body>
</html>
Output :
The PHP Time() Function
The time() function returns the current timestamp. The timestamp that you see in the previous paragraph is generated by a very simple code :
Basic Time Syntax
<!DOCTYPE html>
<html>
<head>
    <title>Basic Time Syntax</title>
</head>
<body>

	<?php
		date_default_timezone_set("Asia/Kolkata");
		echo "The time is " . date("h:i:sa");
	?>

</body>
</html>
Output :

If the time you got back from the code is not the right time, it's probably because your server is in another country or set up for a different timezone.

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)

PHP strtotime() Function
The strtotime() function accepts an English datetime description and turns it into a timestamp. It is a simple way to determine "tomorrow" or "next week" or "+3 Months" etc., without using the time() function and a bunch of math.
strtotime() Function Example :
<!DOCTYPE html>
<html>
<head>
    <title>PHP strtotime() Function</title>
</head>
<body>

	<?php
	
		date_default_timezone_set("Asia/Kolkata");
	
		$d=strtotime("tomorrow");
		echo date("Y-m-d h:i:sa", $d) . "<br />";
		
		$d=strtotime("next Saturday");
		echo date("Y-m-d h:i:sa", $d) . "<br />";
		
		$d=strtotime("+3 Months");
		echo date("Y-m-d h:i:sa", $d) . "<br />";
		
		$d=strtotime("10:44 AM September 18 2017");
		echo "Created date is :" . date("Y-m-d h:i:sa", $d);
	?>

</body>
</html>
Output :
PHP Date/Time Functions

The PHP date and time functions are following table formate.

Function Description
checkdate() Validates a Gregorian date
date_add() Adds an amount of days, months, years, hours, minutes and seconds to a date
date_create_from_format() Returns a new DateTime object formatted according to the specified format
date_create() Returns new DateTime object
date_date_set() Sets a new date
date_default_timezone_get() Returns the default timezone used by all date/time functions in a script
date_default_timezone_set() Sets the default timezone used by all date/time functions in a script
date_diff() Returns the difference between two dates
date_format() Returns a date formatted according to a specified format
date_get_last_errors() Returns the warnings and errors found while parsing a date/time string
date_interval_create_from_date_string() Sets up a DateInterval from the relative parts of the string
date_interval_format() Formats the interval
date_isodate_set() Set a date according to the ISO 8601 standard
date_modify() Modifies the timestamp
date_offset_get() Returns the timezone offset
date_parse_from_format() Returns an associative array with detailed info about given date formatted according to the specified format
date_parse() Returns associative array with detailed info about a specified date
date_sub() Subtracts an amount of days, months, years, hours, minutes and seconds from a date
date_sun_info() Returns an array with information about sunset/sunrise and twilight begin/end for a specified day and location
date_sunrise() Returns time of sunrise for a given day and location
date_sunset() Returns time of sunset for a given day and location
date_time_set() Sets the time
date_timestamp_get() Returns the Unix timestamp representing the date
date_timestamp_set() Sets the date and time based on an Unix timestamp
date_timezone_get() Return time zone relative to given DateTime
date_timezone_set() Sets the time zone for the DateTime object
date() Formats a local date and time
getdate() Returns date/time information of the timestamp or the current local date/time
gettimeofday() Returns the current time
gmdate() Formats a GMT/UTC date and time
gmmktime() Get Unix timestamp for a GMT date
gmstrftime() Formats a GMT/UTC date and time according to locale settings
idate() Formats a local time/date as integer
localtime() Returns the local time
microtime() Return the current Unix timestamp with microseconds
mktime() Returns the Unix timestamp for a date
strftime() Formats a local time/date according to locale settings
strptime() Parses a time/date generated with strftime()
strtotime() Parses an English textual datetime into a Unix timestamp
time() Returns the current time as a Unix timestamp
timezone_abbreviations_list() Returns associative array containing dst, offset and the timezone name
timezone_identifiers_list() Returns an indexed array containing all defined timezone identifiers
timezone_location_get() Returns the location information for a specified timezone
timezone_name_from_abbr() Returns the timezone name from abbreviation
timezone_name_get() Returns the name of the timezone
timezone_offset_get() Returns the timezone offset from GMT
timezone_open() Creates new DateTimeZone object
timezone_transitions_get() Returns all transitions for the timezone
timezone_version_get() Returns the current version of the timezonedb