The PHP date() function is used to format a date and/or a time.
Syntax:
<?php date(format,[timestamp]); ?>
Here,
- “date(…)” 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” for PHP date format YYYY-MM-DD
- “Y” to display the current year
- “[timestamp]” is optional. If no timestamp has been provided, PHP will get the use of the PHP current date-time on the server.
The required format parameter of the date() function specifies how to format the date or time.
Here are some characters that are commonly used for dates:
- 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.
Let’s see the example to format today’s date in 3 different ways:
<!DOCTYPE html> <html> <body> <?php echo date("d/m/Y") . "<br>"; echo date("d-m-Y") . "<br>"; echo date("d.m.Y") . "<br>"; echo date("l"); ?> </body> </html>
You can use the PHP date() function to automatically update the copyright duration on your website, like:
Copyright © 2010-<?php echo date(“Y”)?>
Similarly, you can use the following characters to format the time string:
- h – Represent hour in 12-hour format with leading zeros (01 to 12)
- H – Represent hour in 24-hour format with leading zeros (00 to 23)
- i – Represent minutes with leading zeros (00 to 59)
- s – Represent seconds with leading zeros (00 to 59)
- a – Represent lowercase ante meridiem and post meridiem (am or pm)
- A – Represent uppercase Ante meridiem and Post meridiem (AM or PM)
Let’s see the example below output the current time in the specified format:
<!DOCTYPE html> <html> <body> <?php echo "The time is " . date("h:i:sa"); ?> </body> </html>
Let’s see another example that displays the date in different formats:
<!DOCTYPE html> <html> <body> <?php echo date("h:i:s") . "<br>"; echo date("F d, Y h:i:s A") . "<br>"; echo date("h:i a"); ?> </body> </html>
The PHP time() Function
The time() function is used to get the current time as a Unix timestamp (the number of seconds since the beginning of the Unix epoch: January 1 1970 00:00:00 GMT).
Let’s see the example:
<!DOCTYPE html> <html> <body> <?php /* Return current date and time from the remote server as timestamp */ $timestamp = time(); echo($timestamp); ?> </body> </html>
Get Your Time Zone
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.
So, if you need the time to be correct according to a specific location, you can set the timezone you want to use.
The example below sets the timezone to “America/New_York”, then outputs the current time in the specified format:
<!DOCTYPE html> <html> <body> <?php date_default_timezone_set("Asia/Calcutta"); echo "The time is " . date("h:i:sa"); ?> </body> </html>
The PHP mktime() Function
The mktime() function is used to create the timestamp based on a specific date and time. If no date and time is provided, the timestamp for the current date and time is returned.
The syntax of the mktime() function can be given with:
mktime(hour, minute, second, month, day, year)
The following example displays the timestamp corresponding to 3:20:12 pm on May 10, 2014:
<!DOCTYPE html> <html> <body> <?php // Create the timestamp for a particular date echo mktime(15, 20, 12, 5, 10, 2014); ?> </body> </html>
Note: You can leave out as many arguments as you like, and the value corresponding to the current time will be used instead. If you omit all the arguments, the mktime() function will return the UNIX timestamp corresponding to the current date and time, just like time().