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

后端 未结 30 1594
孤城傲影
孤城傲影 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:

    <?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");
    ?>
    

    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
    0 讨论(0)
  • 2020-11-22 08:57
    // Simply:
    $date = date('Y-m-d H:i:s');
    
    // Or:
    $date = date('Y/m/d H:i:s');
    
    // This would return the date in the following formats respectively:
    $date = '2012-03-06 17:33:07';
    // Or
    $date = '2012/03/06 17:33:07';
    
    /** 
     * This time is based on the default server time zone.
     * If you want the date in a different time zone,
     * say if you come from Nairobi, Kenya like I do, you can set
     * the time zone to Nairobi as shown below.
     */
    
    date_default_timezone_set('Africa/Nairobi');
    
    // Then call the date functions
    $date = date('Y-m-d H:i:s');
    // Or
    $date = date('Y/m/d H:i:s');
    
    // date_default_timezone_set() function is however
    // supported by PHP version 5.1.0 or above.
    

    For a time-zone reference, see List of Supported Timezones.

    0 讨论(0)
  • 2020-11-22 08:57

    According to the article How to Get Current Datetime (NOW) with PHP, there are two common ways to get the current date. To get current datetime (now) with PHP, you can use the date class with any PHP version, or better the datetime class with PHP >= 5.2.

    Various date format expressions are available here.

    Example using date

    This expression will return NOW in format Y-m-d H:i:s.

    <?php
        echo date('Y-m-d H:i:s');
    ?>
    

    Example using datetime class

    This expression will return NOW in format Y-m-d H:i:s.

    <?php
        $dt = new DateTime();
        echo $dt->format('Y-m-d H:i:s');
    ?>
    
    0 讨论(0)
  • 2020-11-22 08:57
    echo date('y-m-d'); // Today
    

    This will return today's date.

    0 讨论(0)
  • 2020-11-22 09:01

    You can either use the $_SERVER['REQUEST_TIME'] variable (available since PHP 5.1.0) or the time() function to get the current Unix timestamp.

    0 讨论(0)
  • 2020-11-22 09:02

    You can use this code:

    <?php
        $currentDateTime = date('Y-m-d H:i:s');
        echo $currentDateTime;
    ?>
    
    0 讨论(0)
提交回复
热议问题