How do I get the current date and time in PHP?

后端 未结 30 1604
孤城傲影
孤城傲影 2020-11-22 08:19

Which PHP function can return the current date/time?

30条回答
  •  渐次进展
    2020-11-22 08:55

    date(format, timestamp)
    

    The date function returns a string formatted according to the given format string using the given integer timestamp or the current time if no timestamp is given. In other words, timestamp is optional and defaults to the value of time().

    And the parameters are -

    format - Required. Specifies the format of the timestamp

    timestamp - (Optional) Specifies a timestamp. Default is the current date and time

    How to get a simple date

    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:

    1. d - Represents the day of the month (01 to 31)
    2. m - Represents a month (01 to 12)
    3. Y - Represents a year (in four digits)
    4. l (lowercase 'L') - Represents the day of the week

    Other characters, like "/", ".", or "-" can also be inserted between the characters to add additional formatting.

    The example below formats today's date in three different ways:

    ";
        echo "Today is " . date("Y.m.d") . "
    "; echo "Today is " . date("Y-m-d") . "
    "; echo "Today is " . date("l"); ?>

    Some useful links

    • gmdate() - Format a GMT/UTC date/time
    • idate() - Format a local time/date as integer
    • getdate() - Get date/time information
    • getlastmod() - Gets time of last page modification
    • mktime() - Get Unix timestamp for a date
    • strftime() - Format a local time/date according to locale settings
    • time() - Return current Unix timestamp
    • strtotime() - Parse about any English textual datetime description into a Unix timestamp
    • Predefined DateTime Constants

提交回复
热议问题