Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

php

datetime

People also ask

How can I get current date variable in PHP?

use the builtin date() function. $myDate = date('m/d/Y');

How can I get current date and time in MySQL and PHP?

MySQL SYSDATE() returns the current date and time in YYYY-MM-DD HH:MM:SS or YYYYMMDDHHMMSS. uuuuuu format depending on the context of the function. MySQL TIME_FORMAT() converts a time in a formatted string using the format specifiers. MySQL TIME_TO_SEC() converts a time value in to seconds.

How can I get current date in YYYY-MM-DD format in PHP?

$date = date("yyyy-mm-dd", strtotime(now));

What does NOW () return in PHP?

It is a PHP function that "returns the date & time in the same format as the MySQL function NOW()", which is precisely the question.


The time would go by your server time. An easy workaround for this is to manually set the timezone by using date_default_timezone_set 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 :)


// 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.


Since PHP 5.2.0 you can use the DateTime() class:

use \Datetime;

$now = new DateTime();
echo $now->format('Y-m-d H:i:s');    // MySQL datetime format
echo $now->getTimestamp();           // Unix Timestamp -- Since PHP 5.3

And to specify the timezone:

$now = new DateTime(null, new DateTimeZone('America/New_York'));
$now->setTimezone(new DateTimeZone('Europe/London'));    // Another way
echo $now->getTimezone();

Reference: Here's a link

This can be more reliable than simply adding or subtracting the number of seconds in a day or a month to a timestamp because of daylight saving time.

The PHP code

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

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');

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.


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());