w3pop.com :: ÍøÂçѧԺ :: PHP :: PHP Date
The PHP date() function is used to format a time or a date.
PHP date()º¯ÊýµÄ×÷ÓÃÊǶ¨ÒåÒ»¸öʱ¼ä[time]»òÈÕÆÚ[date]¡£
The PHP date() function formats a timestamp to a more readable date and time.
PHP date()º¯ÊýµÄ×÷ÓÃÊǸøÈÕÆÚºÍʱ¼ä¶¨ÒåÒ»¸ö“timestamp”¡£
date(format,timestamp) |
| Parameter ²ÎÊý |
Description ÃèÊö |
|---|---|
| format | Required. Specifies the format of the timestamp ±ØÒª²ÎÊý¡£Ö¸¶¨timestampµÄ¸ñʽ£¨format of the timestamp£© |
| timestamp | Optional. Specifies a timestamp. Default is the current date and time (as a timestamp) ¿ÉÑ¡²ÎÊý¡£Ö¸¶¨Ò»¸ötimestamp¡£Ä¬ÈÏÖµÊǵ±Ç°ÈÕÆÚºÍʱ¼ä£¨ÒÔ´Ë×÷Ϊtimestamp£© |
A timestamp is the number of seconds since January 1, 1970 at 00:00:00 GMT. This is also known as the Unix Timestamp.
Timestamp£º´Ó1970Äê1ÔÂ1ÈÕÆð¸ñÁÖÄáÖαê׼ʱ¼ä£¨GMT£©¹æ¶¨ÁËÒ»ÃëÖӵıê×¼µ±Á¿£¬timestamp¾ÍÊÇÖ¸ÒÔÕâ¸öΪ±ê×¼µÄ×ܵÄÃëÊý¡£ÕâÀàËÆÓÚUnix Timestamp¡£
The first parameter in the date() function specifies how to format the date/time. It uses letters to represent date and time formats. Here are some of the letters that can be used:
Date()º¯ÊýÖеĵÚÒ»¸ö²ÎÊý¶¨ÒåÁËÈÕÆÚ/ʱ¼äµÄ¸ñʽ¡£ËüÊÇÓÃ×ÖĸÀ´±íʾÈÕÆÚ/ʱ¼äµÄ¸ñʽ¡£ÏÂÃæ¶ÔÕâЩ×Öĸ×÷¾ßÌå˵Ã÷£º
An overview of all the letters that can be used in the format parameter, can be found in our PHP Date reference.
¹ØÓÚÈÕÆÚ/ʱ¼ä¸ñʽµÄËùÓвÎÊý¿ÉÒÔ²ÎÔÄÎÒÃǵÄPHP Date ²ÎÊý¡£
Other characters, like"/", ".", or "-" can also be inserted between the letters to add additional formatting:
ÆäËû×Ö·û£¬È磺"/" "." "-" Ò²¿ÉÒÔ²åÈëÉÏÊöµÄÈÕÆÚ/ʱ¼ä¸ñʽ×Öĸ֮¼äÀ´¶¨ÒåÏÔʾ¸ñʽ£¬¾ßÌåÈçÏ£º
<?php |
The output of the code above could be something like this:
ÉÏÊö´úÂ뽫Êä³öÏÂÃæµÄ½á¹û£º
2006/07/11 |
The second parameter in the date() function specifies a timestamp. This parameter is optional. If you do not supply a timestamp, the current time will be used.
Date()º¯ÊýÖеĵڶþ¸ö²ÎÊýÖ¸¶¨ÁËÒ»¸ötimestamp¡£Õâ¸ö²ÎÊýÊÇÒ»¸ö¿ÉÑ¡£¨optional£©²ÎÊý¡£Èç¹ûÄã²»Ö¸¶¨Õâ¸ö²ÎÊý£¬ÄÇô½«Ä¬ÈÏʹÓõ±Ç°Ê±¼ä¡£
In our next next example we will use the mktime() function to create a timestamp for tomorrow.
ÔÚÏÂÃæµÄÀý×ÓÖУ¬ÎÒÃǽ«Ê¹ÓÃmktime()º¯ÊýΪÃ÷Ìì´´½¨Ò»¸ötimestamp¡£
The mktime() function returns the Unix timestamp for a specified date.
Mktime()º¯ÊýΪָ¶¨µÄÈÕÆÚ·µ»ØÁËUnix timestamp¡£
mktime(hour,minute,second,month,day,year,is_dst) |
To go one day in the future we simply add one to the day argument of mktime():
ÎÒÃÇͨ¹ý¸ømktime()ÖеÄday×Ô±äÁ¿£¨argument£©¼ÓÈëÒ»¸ötimestampÖ±½Óǰ½øµ½½«À´µÄijһÌ죬¾ßÌåÈçÏ£º
<?php |
The output of the code above could be something like this:
ÉÏÊö´úÂ뽫Êä³öÏÂÃæµÄ½á¹û£º
Tomorrow is 2006/07/12 |
For more information about all the PHP date functions, please visit our PHP Date Reference.
¾ßÌåµÄPHPÈÕÆÚ²ÎÊýÇë·ÃÎÊÎÒÃǵÄPHP Date ²ÎÊý¡£