views:

6693

answers:

6

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

+13  A: 

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.

Gumbo
+7  A: 

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');
Rob Hruska
the 2nd argument of the date function is assumed to be time() if left empty.
Paolo Bergantino
+1  A: 

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());
Shane Larson
A: 

$date = date('m/d/Y h:i:s a', time());

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

What I mean is the time comes back EST when I'm PST... but why? Is it the server time and the server is EST? Can I get users time? Their server may be another time zone, no?
Look into the manual: http://docs.php.net/manual/en/function.date.php
Gumbo
The date function is the time of your server, either check it, or temporarily set it /w PHP before you run date.
TravisO
thanks, my service must not be up to date with php because O works for GMT but not P or "e" time zone... thanks!!
This $date = date('m/d/Y h:i:s a', time()); gets the server date but still doesn't get me the user date. This is showing me at EST when I'm at PST. I want to get the date the user sends the form and like me their server may be in a different time zone.
Actually now that I think about it the time isn't as important as the date. THANKS TO ALL!!
+3  A: 

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 :)

NJ
The call to time() is redundant, date() will automatically use the current time.
too much php
A: 

how display time and date for whenever file was made??? pls give me answer..