哪个PHP函数可以返回当前日期/时间?


当前回答

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

其他回答

日期格式也取决于:

echo date("d/m/Y H:i:sa"); // 13/04/2017 19:38:15pm
<?php
echo "<b>".date('l\, F jS\, Y ')."</b>";
?>

像这样的指纹

2012年12月9日,星期天

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

这段代码将获得运行该代码的服务器的日期和时间。

非常简单的

date_default_timezone_set('Asia/Kolkata');
$date = date('m/d/Y H:i:s', time());

对于新的PHP程序员来说,他们可能会困惑为什么有这么多方法来获取当前日期和时间,以及在他们的项目中使用哪一种方法。

1. date方法(PHP 4, PHP 5, PHP 7)

这是php中获取日期和时间的最常见和最简单的方法。

// 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 2005 03:12:46 PM
echo date('l jS \of F Y h:i:s A');

// Prints: July 1, 2000 is on a Saturday
echo "July 1, 2000 is on a " . date("l", mktime(0, 0, 0, 7, 1, 2000));

/* 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: 2000-07-01T00:00:00+00:00
echo date(DATE_ATOM, mktime(0, 0, 0, 7, 1, 2000));

你可以在这里了解更多

2. DateTime类(PHP 5 >= 5.2.0, PHP 7)

当你想在OOP中使用PHP时,这是获取日期和时间的最佳方式。

<?php
// Specified date/time in your computer's time zone.
$date = new DateTime('2000-01-01');
echo $date->format('Y-m-d H:i:sP') . "\n";

// Specified date/time in the specified time zone.
$date = new DateTime('2000-01-01', new DateTimeZone('Pacific/Nauru'));
echo $date->format('Y-m-d H:i:sP') . "\n";

// Current date/time in your computer's time zone.
$date = new DateTime();
echo $date->format('Y-m-d H:i:sP') . "\n";

// Current date/time in the specified time zone.
$date = new DateTime(null, new DateTimeZone('Pacific/Nauru'));
echo $date->format('Y-m-d H:i:sP') . "\n";

// Using a UNIX timestamp.  Notice the result is in the UTC time zone.
$date = new DateTime('@946684800');
echo $date->format('Y-m-d H:i:sP') . "\n";

// Non-existent values roll over.
$date = new DateTime('2000-02-30');
echo $date->format('Y-m-d H:i:sP') . "\n";
?>

你可以在这里了解更多

3.碳日期时间套餐

如果你正在使用Composer, Laravel, Symfony或任何类型的框架,这是获得日期和时间的最佳方式。此外,这个包扩展了php中的DateTime类,因此您可以使用DateTime类中的所有方法。 这个内置的框架像laravel,所以你不需要单独安装它。

printf("Right now is %s", Carbon::now()->toDateTimeString());
printf("Right now in Vancouver is %s", Carbon::now('America/Vancouver')); // automatically converted to string
$tomorrow = Carbon::now()->addDay();
$lastWeek = Carbon::now()->subWeek();

// Carbon embed 823 languages:
echo $tomorrow->locale('fr')->isoFormat('dddd, MMMM Do YYYY, h:mm');
echo $tomorrow->locale('ar')->isoFormat('dddd, MMMM Do YYYY, h:mm');

$officialDate = Carbon::now()->toRfc2822String();

$howOldAmI = Carbon::createFromDate(1975, 5, 21)->age;

$noonTodayLondonTime = Carbon::createFromTime(12, 0, 0, 'Europe/London');

$internetWillBlowUpOn = Carbon::create(2038, 01, 19, 3, 14, 7, 'GMT');

if (Carbon::now()->isWeekend()) {
    echo 'Party!';
}
echo Carbon::now()->subMinutes(2)->diffForHumans(); // '2 minutes ago'

你可以在这里了解更多

希望这对你有所帮助,如果你知道任何其他获取日期和时间的方法,请随意编辑答案。