PHP Date and Time
- Previous Page PHP Multidimensional Arrays
- Next Page PHP Include
The PHP date() function is used to format dates or times.
PHP Date() function
The PHP Date() function formats the timestamp into a more readable date and time.
Syntax
date(format,timestamp)
Parameter | Description |
---|---|
format | Required. Specifies the format of the timestamp. |
timestamp | Optional. Specifies the timestamp. The default is the current time and date. |
Note:Timestamp is a character sequence that represents the date and time of a specific event.
Get simple date
The format parameter of the date() function is required, which specifies how to format the date or time.
The following lists some commonly used characters for dates:
- d - Represents the day of the month (01-31)
- m - Represents the month (01-12)
- Y - Represents the year (four digits)
- 1 - Represents a day of the week
Other characters, such as "/", "." or "-", can also be inserted into characters to increase other formats.
The following examples use three different methods to format today's date:
Example
<?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"); ?>
PHP Tip - Automatic copyright year
Use the date() function to automatically update the version year on your website:
Example
© 2010-<?php echo date("Y")?>
Get simple time
The following are commonly used time characters:
- h - 12-hour format with leading zero
- i - minutes with leading zero
- s - seconds with leading zero (00-59)
- a - lowercase ante meridiem and post meridiem (am or pm)
The following example outputs the current time in a specified format:
Example
<?php echo "The current time is " . date("h:i:sa"); ?>
Note:Note that the PHP date() function will return the current date/time of the server!
Get the time zone
If the time returned from the code is not correct, it may be because your server is located in another country or has been set to a different time zone.
Therefore, if you need accurate time based on a specific location, you can set the time zone you want to use.
The following example sets the time zone to "Asia/Shanghai" and then outputs the current time in a specified format:
Example
<?php date_default_timezone_set("Asia/Shanghai"); echo "The current time is " . date("h:i:sa"); ?>
Create a date using PHP mktime()
The optional timestamp parameter in the date() function specifies the timestamp. If you do not specify a timestamp, it will use the current date and time (as in the example above).
The mktime() function returns the Unix timestamp of the date. Unix timestamp is the number of seconds between the Unix epoch (January 1, 1970 00:00:00 GMT) and the specified time.
Syntax
mktime(hour,minute,second,month,day,year)
The following example creates a date and time using a series of parameters from the mktime() function:
Example
<?php $d=mktime(9, 12, 31, 6, 10, 2015); echo "The creation date is " . date("Y-m-d h:i:sa", $d); ?>
Create a date using a string with PHP strtotime()
The PHP strtotime() function is used to convert human-readable strings to Unix time.
Syntax
strtotime(time,now)
The following example creates a date and time using the strtotime() function:
Example
<?php $d=strtotime("10:38pm April 15 2015"); echo "The creation date is " . date("Y-m-d h:i:sa", $d); ?>
PHP is very smart in converting strings to dates, so you can use various values:
Example
<?php $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>"; ?>
However, strtotime() is not perfect, so please remember to check the strings placed within it.
More Date Examples
The following example outputs the date of next Saturday:
Example
<?php $startdate = strtotime("Saturday"); $enddate = strtotime("+6 weeks",$startdate); while ($startdate < $enddate) { echo date("M d", $startdate),"<br>"; $startdate = strtotime("+1 week", $startdate); } ?>
The following example outputs the number of days before July 4th:
Example
<?php $d1=strtotime("December 31"); $d2=ceil(($d1-time())/60/60/24); echo "Days until December 31st: " . $d2 ." days."; ?>
Complete PHP Date Reference Manual
For a complete manual of all date functions, please visit our PHP Date Reference Manual.
This manual includes a brief description and usage examples for each function.
- Previous Page PHP Multidimensional Arrays
- Next Page PHP Include