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

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

Which PHP function can return the current date/time?

相关标签:
30条回答
  • 2020-11-22 08:44

    its very simple

    echo $date = date('Y-m-d H:i:s');

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

    The date format depends too:

    echo date("d/m/Y H:i:sa"); // 13/04/2017 19:38:15pm
    
    0 讨论(0)
  • 2020-11-22 08:45

    I found that the simplest way of getting the current time in PHP is something like this.

    //Prints out something like 10:00am Just be sure to set your timezone correctly.
    date_default_timezone_set("America/Chicago");
    $TIME = date('G:ia'); 
    
    0 讨论(0)
  • 2020-11-22 08:45
    // Set the default timezone to use. Available since PHP 5.1
    date_default_timezone_set('UTC');
    
    
    // Prints something like: Monday
    echo date("l");
    
    // Prints something like: Monday 8th of August 2016 03:12:46 PM
    echo date('l jS \of F Y h:i:s A');
    
    // Prints: July 1, 2016 is on a Saturday
    echo "July 1, 2016 is on a " . date("l", mktime(0, 0, 0, 7, 1, 2016));
    
    /* Use the constants in the format parameter */
    // Prints something like: Wed, 25 Sep 2013 15:28:57 -0700
    echo date(DATE_RFC2822);
    
    // Prints something like: 2016-07-01T00:00:00+00:00
    echo date(DATE_ATOM, mktime(0, 0, 0, 7, 1, 2000));
    
    0 讨论(0)
  • 2020-11-22 08:46

    You can use both the $_SERVER['REQUEST_TIME'] variable or the time() function. Both of these return a Unix timestamp.

    Most of the time these two solutions will yield the exact same Unix Timestamp. The difference between these is that $_SERVER['REQUEST_TIME'] returns the time stamp of the most recent server request and time() returns the current time. This may create minor differences in accuracy depending on your application, but for most cases both of these solutions should suffice.

    Based on your example code above, you are going to want to format this information once you obtain the Unix Timestamp. Unformatted Unix time looks like: 1232659628

    So in order to get something that will work, you can use the date() function to format it.

    A good reference for ways to use the date() function is located in the PHP Manual.

    As an example, the following code returns a date that looks like this: 01/22/2009 04:35:00 pm :

    echo date("m/d/Y h:i:s a", time());
    
    0 讨论(0)
  • 2020-11-22 08:46
    <?php
    // Assuming today is March 10th, 2001, 5:16:18 pm, and that we are in the
    // Mountain Standard Time (MST) Time Zone
    
    $today = date("F j, Y, g:i a");                 // March 10, 2001, 5:16 pm
    $today = date("m.d.y");                         // 03.10.01
    $today = date("j, n, Y");                       // 10, 3, 2001
    $today = date("Ymd");                           // 20010310
    $today = date('h-i-s, j-m-y, it is w Day');     // 05-16-18, 10-03-01, 1631 1618 6 Satpm01
    $today = date('\i\t \i\s \t\h\e jS \d\a\y.');   // it is the 10th day.
    $today = date("D M j G:i:s T Y");               // Sat Mar 10 17:16:18 MST 2001
    $today = date('H:m:s \m \i\s\ \m\o\n\t\h');     // 17:03:18 m is month
    $today = date("H:i:s");                         // 17:16:18
    $today = date("Y-m-d H:i:s");                   // 2001-03-10 17:16:18 (the MySQL DATETIME format)
    ?>
    
    0 讨论(0)
提交回复
热议问题