ÍøÂçѧԺ w3popÉçÇø ÍøÂç×ÊÔ´ ITÐÂÎÅ

w3pop.com :: ÍøÂçѧԺ :: PHP :: PHP Date

»áÔ±µÇ½

ÕʺÅ

ÃÜÂë

»Ø´ð

¼ÇסÃÜÂë

Íü¼ÇÃÜÂë? ×¢²á

PHP
WINDOWSϰ²×°MyS..
PHP ÖÆ×÷ ÍøÕ¾/·þ..
ÓÃPHPºÍCSSÖÆ×÷»î..
PHP µ¥¼þģʽ
PHP MVCģʽ£¬Àà·..
PHP ÖÐʹÓÃÕýÔò±í..
PHP ·ÀÖ¹ SQL ×¢È..
PHP ¿çÕ¾µã½Å±¾¹¥..
PHP ·ÀÖ¹Óû§²Ù×Ý..
PHP ·ÀÖ¹Ô¶³Ì±íµ¥..

PHP Date


×÷Õß:w3pop.com ·­Òë/ÕûÀí:w3pop.com ·¢²¼:2007-04-28 ÐÞ¸Ä:2007-06-17 ä¯ÀÀ:8302 :: ::

The PHP date() function is used to format a time or a date.
PHP date()º¯ÊýµÄ×÷ÓÃÊǶ¨ÒåÒ»¸öʱ¼ä[time]»òÈÕÆÚ[date]¡£


The PHP Date() Function
PHP Date()º¯Êý

The PHP date() function formats a timestamp to a more readable date and time.
PHP date()º¯ÊýµÄ×÷ÓÃÊǸøÈÕÆÚºÍʱ¼ä¶¨ÒåÒ»¸ö“timestamp”¡£

Syntax
Óï·¨

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£©

 


PHP Date - What is a Timestamp?
PHP ÈÕÆÚ – ¾¿¾¹Ê²Ã´ÊÇ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¡£


PHP Date - Format the Date
PHP ÈÕÆÚdate()—— ¶¨ÒåÈÕÆÚ

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()º¯ÊýÖеĵÚÒ»¸ö²ÎÊý¶¨ÒåÁËÈÕÆÚ/ʱ¼äµÄ¸ñʽ¡£ËüÊÇÓÃ×ÖĸÀ´±íʾÈÕÆÚ/ʱ¼äµÄ¸ñʽ¡£ÏÂÃæ¶ÔÕâЩ×Öĸ×÷¾ßÌå˵Ã÷£º

  • d - The day of the month (01-31)
    d – ÿÔ°üº¬µÄÌìÊý£¨01-31£©
  • m - The current month, as a number (01-12)
    m – µ±Ç°Ô·ݣ¬ÓÃÊý×Ö£¨01-12£©±íʾ
  • Y - The current year in four digits
    Y – µ±Ç°µÄÄê·Ý£¬ÓÃËÄλÊý×Ö±íʾ

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
echo date("Y/m/d");
echo "<br />";
echo date("Y.m.d");
echo "<br />";
echo date("Y-m-d");
?>

The output of the code above could be something like this:
ÉÏÊö´úÂ뽫Êä³öÏÂÃæµÄ½á¹û£º

2006/07/11
2006.07.11
2006-07-11

 


PHP Date - Adding a Timestamp
PHP ÈÕÆÚ£¨Date£©- Ìí¼ÓÒ»¸öTimestamp

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¡£

Syntax
Óï·¨

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
$tomorrow = mktime(0,0,0,date("m"),date("d")+1,date("Y"));
echo "Tomorrow is ".date("Y/m/d/", $tomorrow);
?>

The output of the code above could be something like this:
ÉÏÊö´úÂ뽫Êä³öÏÂÃæµÄ½á¹û£º

Tomorrow is 2006/07/12

 


PHP Date - Reference
PHP Date – ²ÎÊý

For more information about all the PHP date functions, please visit our PHP Date Reference.
¾ßÌåµÄPHPÈÕÆÚ²ÎÊýÇë·ÃÎÊÎÒÃǵÄPHP Date ²ÎÊý¡£

ÆÀÂÛ (4) 1 All