How to get the current date and time in PHP?

2018-12-31 18:27发布

问题:

Which PHP function can return the current date/time?

回答1:

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



回答2:

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



回答3:

Since PHP 5.2.0 you can do it using OOP and DateTime() as well (of course if you prefer OOP):

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


回答4:

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)


回答5:

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.



回答6:

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


回答7:

PHP\'s date function can do this job

date()

Description :

string date ( string $format [, int $timestamp = time() ] )

Returns a string formatted according to the given format string using the given integer timestamp or the current time if no timestamp is given.

Examples :

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


回答8:

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


回答9:

 $date = new DateTime(\'now\', new DateTimeZone(\'Asia/Kolkata\'));
 echo $date->format(\'d-m-Y H:i:s\');

Update

 //Also get am/pm in datetime:
 echo $date->format(\'d-m-Y H:i:s a\'); // output 30-12-2013 10:16:15 am

For the date format, PHP date() Function is useful.



回答10:

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

works, but how also to know if it\'s EST, PST?



回答11:

echo date(\"d-m-Y H:i:sa\");

This code will get the date and time of the server that the code runs on.



回答12:

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


回答13:

You can use this format also

$date = date(\"d-m-Y\");

or

$date = date(\"Y-m-d H:i:s\"); 


回答14:

<?php
echo \"<b>\".date(\'l\\, F jS\\, Y \').\"</b>\";
?>

Prints like this

Sunday, December 9th, 2012



回答15:

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


回答16:

its very simple

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



回答17:

If you want a different timescale, please use:

$tomorrow  = mktime(0, 0, 0, date(\"m\")  , date(\"d\")+1, date(\"Y\"));
$lastmonth = mktime(0, 0, 0, date(\"m\")-1, date(\"d\"),   date(\"Y\"));
$nextyear  = mktime(0, 0, 0, date(\"m\"),   date(\"d\"),   date(\"Y\")+1);

date_default_timezone_set(\"Asia/Calcutta\");
echo date(\"Y/m/d H:i:s\");


回答18:

Set your time zone:

date_default_timezone_set(\'Asia/Calcutta\');

Then call the date functions

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


回答19:

date_default_timezone_set(\'Europe/Warsaw\');
echo(\"<p class=\'time\'>\".date(\'H:i:s\').\"</p>\");
echo(\"<p class=\'date\'>\".date(\'d/m/Y\').\"</p>\");


回答20:

date(format,timestamp)

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


回答21:

Date Format depends too :

echo date(\"d/m/Y H:i:sa\");    // 13/04/2017 19:38:15pm


回答22:

You can use this code.

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


回答23:

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


回答24:

Another simple way to take timestamp of current date and time use mktime() function

$now = mktime(); //return timestamp of current time

then you can convert this to another date format:

//// Prints something like: Thursday 26th of January 2017 01:12:36 PM
echo date(\'l jS \\of F Y h:i:s A\',$now);

more date formats here: http://php.net/manual/en/function.date.php



回答25:

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


回答26:

If you are Bangladeshi. And if you want get time of Dhaka then use it

$date = new DateTime();
$date->setTimeZone(new DateTimeZone(\"Asia/Dhaka\"));
$get_datetime = $date->format(\'d.m.Y H:i:s\');


回答27:

That\'s May Help you.

Here are some characters that are commonly used for times:

  1. h - 12-hour format of an hour with leading zeros (01 to 12)
  2. i - Minutes with leading zeros (00 to 59)
  3. s - Seconds with leading zeros (00 to 59)
  4. a - Lowercase Ante meridiem and Post meridiem (am or pm)

Get Your Time Zone

<?php
date_default_timezone_set(\"America/New_York\");
echo \"The time is \" . date(\"h:i:sa\");
?>

check this out (optional)

<?php
    $d=mktime(11, 14, 54, 8, 12, 2014);
    echo \"Created date is \" . date(\"Y-m-d h:i:sa\", $d);
    ?>

for Date

<?php
    echo \"Today is \" . date(\"Y/m/d\") . ;
    echo \"Today is \" . date(\"Y.m.d\") . ;
    echo \"Today is \" . date(\"Y-m-d\") . ;
    echo \"Today is \" . date(\"l\");
?>

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

Source-W3-Schools



回答28:

If you want to get Date like 12-3-2016 separately each day,month and year values, then copy-paste this code

$day=date(\"d\");
$month=date(\"m\");
$year=date(\"y\");
print \"date\".$day.\"-\".$month.\"-\".$year;


回答29:

 We can use date function and set default timezone.

 <?php

  date_default_timezone_set(\"Asia/Kolkata\");
  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\");
  echo \"The time is \" . date(\"h:i:sa\");

 ?>


回答30:

Best way to get the current time and date is by date function in php

$date = date(\'FORMAT\'); // FORMAT Eg : Y-m-d H:i:s

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

With the UNIX TIMESTAM

$now_date = date(\'FORMAT\', time()); // FORMAT Eg : Y-m-d H:i:s

To set the server time zone

date_default_timezone_set(\'Asia/Calcutta\');

Different time zone list is here



标签: php datetime