With the PHP Date and Time function date(), you can change the way a date or time looks.
The PHP Date() Function
Date() is a function in PHP. With the PHP date() function, a timestamp is turned into a date and time that is easier to read.
Syntax
date(format,timestamp)
Parameter | Description |
---|---|
format | Required. Indicates how the timestamp should be written. |
timestamp | Optional. Specifies a timestamp. The current date and time are used by default. |
Format for Parameter Description
Required. Indicates how the timestamp should be written. Optional. Specifies a timestamp. The current date and time are used by default.
A timestamp is a string of characters that shows the date and/or time when something happened.
Get a Date
The date() function needs a parameter called format to tell it how to format the date (or time).
Here are some dates that often start with these letters:
- d: Indicates the date of the month (01 to 31)
- m stands for a month (01 to 12)
- Y stands for the year (in four digits)
- l (small letter \’L\’) – Shows what day of the week it is
You can also put other characters like \”/\”, \”.\” or \”-\” between the characters to add more formatting.
In the following example, the date of today is shown in three different ways:
<!DOCTYPE html>
<html>
<body><?php
echo \”Date is \” . date(\”Y/m/d\”) . \”<br>\”;
echo \”Date is \” . date(\”Y.m.d\”) . \”<br>\”;
echo \”Date is \” . date(\”Y-m-d\”) . \”<br>\”;
echo \”Today is \” . date(\”l\”);
?></body>
</html>
Automatic Copyright Year with PHP date
<!DOCTYPE html>
<html>
<body>© copyright 2020-<?php echo date(\”Y\”);?>
</body>
</html>
Get a Time
Here are some commonly used characters for times:
H – An hour in the 24-hour format (00 to 23)
h – Format for a 12-hour hour with leading zeros (01 to 12)
I – Minutes that start with a zero (00 to 59)
s – Seconds with zeros in front (00 to 59)
a: Put Ante meridiem and Post meridiem in lowercase (am or pm)
The following example shows how to show the current time:
Example
<?php date(\”h:i:sa\”);?>
<!DOCTYPE html>
<html>
<body><?php
echo \”The time is \” . date(\”h:i:sa\”);
?></body>
</html>
Keep in mind that the PHP date() function will return the server\’s current date and time!
Get Your Time Zone
If the time you got from the code is wrong, it\’s probably because your server is in a different country or is set up for a different time zone.
So, you can set the timezone you want to use if you need the time to be right for a certain place.
The following example sets the timezone to \”Asia/Calcutta\” and then shows the current time in the format you choose:
<!DOCTYPE html>
<html>
<body><?php
date_default_timezone_set(\”Asia/Calcutta\”);
echo \”The time is \” . date(\”h:i:sa\”);
?></body>
</html>
With mktime, you can create a date ()
The timestamp parameter, which is optional, tells the date() function what time it is. If you don\’t include this, the current date and time will be used (as in the examples above).
A date\’s Unix timestamp is what the PHP mktime() function gives back. The Unix timestamp shows the number of seconds between the Unix Epoch (January 1, 1970, 00:00:00 GMT) and the time given.
Syntax
mktime(hour, minute, second, month, day, year) (hour, minute, second, month, day, year);
In the example below, a number of parameters in the mktime() function are used to create a date and time with the date() function:
<!DOCTYPE html>
<html>
<body><?php
$date=mktime(10, 25, 30, 1, 10, 2023);
echo \”Created date is \” . date(\”Y-m-d h:i:sa\”, $date);
?></body>
</html>