Home >Backend Development >PHP Tutorial >PHP date and time
PHP date and time
PHP multidimensional array
PHP Include
PHP date() function is used to format date or time.
PHP Date() Function
PHP Date() function formats a timestamp into a more readable date and time.
Syntax
date(format,timestamp)
Parameter Description
format required. Specifies the format of the timestamp.
timestamp optional. Specify timestamp. The default is the current time and date.
Note: A timestamp is a sequence of characters that represents the date and event when a specific event occurred.
Get a simple date
The format parameters of the date() function are required, they specify how to format the date or time.
Here are 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 the day of the week
Other characters, such as "/", "." or "-" can also be inserted into the characters to add other formatting.
The following example formats today's date in three different ways:
Example
<?php echo "今天是 " . date("Y/m/d") . "<br>"; echo "今天是 " . date("Y.m.d") . "<br>"; echo "今天是 " . date("Y-m-d") . "<br>"; echo "今天是 " . date("l"); ?>
Running Example
PHP Tip - Automatic Copyright Year
Use the date() function to automatically update the version year on your website:
Example
© 2010-3d5bafd22311c7431a2d2dfae05ae577
Run the example
Get simple time
Here are the characters commonly used for time:
h - 12 with leading zero Hours Hour format
i - Minutes with leading zero
s - Seconds with leading zero (00 -59)
a - Lowercase noon and pm (am or pm)
The example below specifies Format output current time:
Instance
<?php echo "现在时间是 " . date("h:i:sa"); ?>
Running example
Note: Please note that the PHP date() function returns the current date/time of the server!
Getting Time Zone
If the time returned from the code is not the correct time, it is possible that your server is located in another country or is set to a different time zone.
So 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 the specified format:
Instance
<?php date_default_timezone_set("Asia/Shanghai"); echo "当前时间是 " . date("h:i:sa"); ?>
Run the instance
Create date via PHP mktime()
Optional in date() function The timestamp parameter specifies the timestamp. If you do not specify a timestamp, the current date and time will be used (as in the example above).
mktime() function returns the Unix timestamp of a date. A Unix timestamp contains 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 uses a series of parameters in the mktime() function to create a date and time:
Instance
<?php $d=mktime(9, 12, 31, 6, 10, 2015); echo "创建日期是 " . date("Y-m-d h:i:sa", $d); ?>
Run the instance
Create date from string via PHP strtotime()
PHP strtotime() function is used to convert human readable string to Unix time.
Syntax
strtotime(time,now)
The following example creates a date and time via the strtotime() function:
Example
<?php $d=strtotime("10:38pm April 15 2015"); echo "创建日期是 " . date("Y-m-d h:i:sa", $d); ?>
Running Example
PHP is very smart at converting strings to dates, so You are able to use various values:
Instance
<?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>"; ?>
Running Example
Strtotime() is not perfect though, so remember to check the strings you put into it.
More date examples
The following example outputs the date of next Saturday:
Instance
<?php $startdate = strtotime("Saturday"); $enddate = strtotime("+6 weeks",$startdate); while ($startdate < $enddate) { echo date("M d", $startdate),"<br>"; $startdate = strtotime("+1 week", $startdate); } ?>
Run the example
The following example outputs the number of days before the Fourth of July:
Example
<?php $d1=strtotime("December 31"); $d2=ceil(($d1-time())/60/60/24); echo "距离十二月三十一日还有:" . $d2 ." 天。"; ?>