The date and time information is internally stored as a 64-bit number, so all conceivably valid 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
The PHP date function is an in-built function that simplifies working with date data types. The PHP date function is used to format a date or time into a human-readable format. For example, you may display the date when an article was published or save the date when the database was last updated.
Syntax:
<?php
date(format,[timestamp]);
?>
HERE,
- date() – the function that returns the current timestamp in PHP on the server.
- format – the general date format that will appear as output.
- Y-m-d – Date format YYYY-MM-DD in PHP.
- Y – the current year.
- m – the current month displayed from months 01 to 12.
- d – the day displayed from days 01 to 31.
- [timestamp] – the time when the action took place. This is optional, and if not specified, PHP will return the current PHP date time on the server.
- To add formatting, characters like”/”, “.” or “-” can be inserted between the numbers for additional formatting.
How to get a date in PHP?
<!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:
Today is : 2022/02/10
Today is : 2022.02.10
Today is : 2022-02-10
Today is : Thursday
Today is : Thu
Today date is : 10
This Month is : Feb
This year is : 2022
Automatic Copyright Year
The date() function is used to automatically update the copyright year on your website:
<!DOCTYPE html>
<html>
<head>
<title>PHP Automatic Copyright Year</title>
</head>
<body>
<p>All © rights reserved 2010 - <?php echo date("Y");?></p>
</body>
</html>
Output:
All © rights reserved 2010 - 2022
The PHP time() Function
The time() function is used to fetch the current time displayed as a Unix timestamp ( with the exact number of seconds from the beginning of the Unix epoch: January 1 1970 00:00:00 GMT).
This timestamp can be converted to a human-readable date by passing it to the previously introduced date() function.
Example:
<?php
// Executed at February 10, 2022 12:33:32
$timestamp = time();
echo($timestamp);
?>
Output:
1644496412000
We can convert this timestamp to a human readable date through passing it to the previously introduce date() function.
Example:
<?php
$timestamp = 1644496412000;
echo(date("F d, Y h:i:s", $timestamp));
?>
Output:
February 10, 2022 12:33:32
How to fetch a simple time?
To fetch a simple time, you should consider the following parameters:
Some commonly used characters used in format parameters are as follows:
- h – hours starting from 01 to 12
- I – minutes starting from 00 to 59
- s – seconds starting from 00 to 59
- a – part of the day, AM or PM.
Example:
<?php
echo "The time is " . date("h:i:sa");
?>
Output:
The time is 10:55:17am
How to verify the time zone?
You can fetch a specific timezone based on a location.
The example below sets the timezone to “Europe/Berlin”, then displays the current time in the specified format.
Example:
<?php
date_default_timezone_set("Europe/Berlin");
echo "The time is " . date("h:i:sa");
?>
Output:
The time is 05:02:46am
PHP Mktime Function
The mktime function returns the timestamp in the Unix format.
Syntax:
<?php
mktime(hour, minute, second, month, day, year, is_dst);
?>
HERE,
- mktime() – the make timestamp function in PHP
- hour – the number of the hour starting from 00 to 12. This is optional.
- minute – the number of minutes starting from 00 to 59. This is optional.
- second – the number of seconds starting from 00 to 59. This is optional.
- month – the number of months starting from 00 to 12. This is optional.
- day – the number of days, starting from 00 to 31. This is optional.
- year – the year. This is optional.
- is_dst – determines the day-saving time (DST). 1 for DST, 0 if not and -1 if unknown.
This example creates a timestamp for the date 01/01/2030 using the mktime function.
<?php
echo mktime(0,0,0,01,01,2030);
?>
HERE,
- 0,0,0 – the hour, minute and seconds, respectively.
- 01 – the day of the month
- 01 – the month of the year
- 2030 – the year
Output:
1893456000
PHP strtotime() Function
PHP’s 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.
Example:
<!DOCTYPE html>
<html>
<head>
<title>PHP strtotime() Function</title>
</head>
<body>
<?php
date_default_timezone_set("Asia/Kolkata");
$d=strtotime("tomorrow");
echo "Tommorrow date is :" . date("Y-m-d h:i:sa", $d) . "<br />";
$d=strtotime("next Saturday");
echo "Next Saturday date is :" . date("Y-m-d h:i:sa", $d) . "<br />";
$d=strtotime("+3 Months");
echo "+3 Months date is :" 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:
Tommorrow date is : 2022-02-11 12:00:00am
Next Saturday date is : 2022-02-12 12:00:00am
+3 Months date is : 2022-05-10 02:57:32pm
Created date is : 2017-09-18 10:44:00am
PHP Date function reference
The table below shows the common parameters used when working with the PHP date functions.
PHP Time parameters
|
|
|
---|---|---|
“ r” |
Returns the full date and time |
|
“ a”,”A” |
Returns the current time, AM or PM respectively |
|
“ g”,”G” |
Returns the hour in numbers from 1 to 12, or 0 to 23, respectively |
|
“ h”,”H” |
Returns the hour in numbers from 01 to 12, or 00 to 23, respectively |
|
“ i”,”s” |
Returns the minutes/seconds in numbers from 00 to 59 |
|
Day parameters
|
|
|
---|---|---|
“ d” |
Returns the day of the month in numbers from 01 to 31 |
|
“ j” |
Returns the day of the month numbers from 1 to 31 |
|
“ D” |
Returns the first three letters of the name of the week day from Sun to Sat |
|
“ l” |
Returns day name of the week from Sunday to Saturday |
|
“ w” |
Returns day of the week in numbers from 0 to 6. Sunday is denoted by zero (0) through to Saturday denoted by six (6) |
|
“ z” |
Returns the day of the year in numbers from 0 to 365 |
|
Month Parameters
|
|
|
---|---|---|
“ m” |
Returns the month number with leading zeroes from 01 to 12 |
|
“ n” |
Returns the month number without leading zeroes from 01 to 12 |
|
“ M” |
Returns the first three letters of the month name Jan to Dec |
|
“ F” |
Returns the month name [January to December] |
|
“ t” |
Returns the number of last four days in a month from 28 to 31 |
|
Year Parameters
|
|
|
---|---|---|
“ L” |
Returns 1 for a leap year and 0 if not |
|
“ Y” |
Returns four digit year format |
|
“ y” |
Returns the year in two (2) digits format from 00 to 99 |
|
Summary:
- The date function in PHP formats the timestamp into the desired format.
- The timestamp is the number of seconds calculated between the current time and the GMT (00:00:00) on 1st January 1970. It is also referred to as the UNIX timestamp.
- All date() functions in PHP use the default time zone which was set in the php.ini file.
- The default time zone can also be programmatically altered using PHP scripts.