Thursday, March 24, 2011

Get current date and time in PHP

What PHP function file can return the current date/time?

From stackoverflow
  • 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.

  • PHP's time() returns a current unix timestamp. With this, you can use the date() function to format it to your needs.

    $date = date('Format String', time());
    

    As Paolo mentioned in the comments, the second argument is redundant. The following snippet is equivalent to the one above:

    $date = date('Format String');
    
    Paolo Bergantino : the 2nd argument of the date function is assumed to be time() if left empty.
  • 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. An unformatted Unix timestamp looks like this...

    Unix Timestamp: 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 Pages, here...

    http://us.php.net/date

    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());
    
  • $date = date('m/d/Y h:i:s a', time());

    Thank you... works, but how also to know if it's EST, PST?

    Gumbo : Look into the manual: http://docs.php.net/manual/en/function.date.php
    TravisO : The date function is the time of your server, either check it, or temporarily set it /w PHP before you run date.
  • The time would go by your server time. An easy workaround for this is to manually set the timezone before the date() or time() functions are called to.

    I'm in Melbourne, Australia so I have something like this:

    date_default_timezone_set('Australia/Melbourne');
    

    Or another example is LA - US:

    date_default_timezone_set('America/Los_Angeles');
    

    You can also see what timezone the server is currently in via:

    date_default_timezone_get();
    

    So something like:

    $timezone = date_default_timezone_get();
    echo "The current server timezone is: " . $timezone;
    

    So the short answer for your question would be:

    // Change the line below to your timezone!
    date_default_timezone_set('Australia/Melbourne');
    $date = date('m/d/Y h:i:s a', time());
    

    Then all the times would be to the timezone you just set :)

    too much php : The call to time() is redundant, date() will automatically use the current time.
  • how display time and date for whenever file was made??? pls give me answer..

0 comments:

Post a Comment