📜  CakePHP-日期和时间

📅  最后修改于: 2020-10-26 05:18:46             🧑  作者: Mango


为了在cakephp4中使用日期和时间,我们将使用可用的FrozenTime类。

要使用日期和时间,请将类包含在控制器中

use Cake\I18n\FrozenTime;

让我们使用FrozenTime类研究示例并显示日期和时间。

如以下程序所示,在config / routes.php文件中进行更改。

config / routes.php

setRouteClass(DashedRoute::class);
$routes->scope('/', function (RouteBuilder $builder) {
   $builder->registerMiddleware('csrf', new CsrfProtectionMiddleware([
      'httpOnly' => true,
   ]));
   $builder->applyMiddleware('csrf');
   //$builder->connect('/pages',['controller'=>'Pages','action'=>'display', 'home']);
   $builder->connect('datetime',['controller'=>'Dates','action'=>'index']);
   $builder->fallbacks();
});

src / Controller / DatesController.php中创建一个DatesController.php文件将以下代码复制到控制器文件中。忽略是否已创建。

src / Controller / DatesController.php

i18nFormat('yyyy-MM-dd HH:mm:ss');
         $this->set('timenow', $_now);
         $now = FrozenTime::parse('now');
         $nice = $now->nice();
         $this->set('nicetime', $nice);
         $hebrewdate = $now->i18nFormat(\IntlDateFormatter::FULL, null, 'en-IR@calendar=hebrew');
         $this->set("hebrewdate",$hebrewdate);
         $japanesedate = $now->i18nFormat(\IntlDateFormatter::FULL, null, 'en-IR@calendar=japanese');
         $this->set("japanesedate",$japanesedate);
         $time = FrozenTime::now();
         $this->set("current_year",$time->year);
         $this->set("current_month",$time->month);
         $this->set("current_day",$time->day);
      }
   }
?>

src / Template上创建目录Dates ,并在该目录下创建一个名为index.php的View文件。将以下代码复制到该文件中。

src / Template / Dates / index.php

";
   echo "Using nice format available = ".$nicetime;
   echo "
"; echo "Date and Time as per Hebrew Calender =" .$hebrewdate; echo "
"; echo "Date and Time as per Japanese Calender =" .$japanesedate; echo "
"; echo "Current Year = ".$current_year; echo "
"; echo "Current Month = ".$current_month; echo "
"; echo "Current Day = ".$current_day; ?>

通过访问以下URL执行以上示例-

http:// localhost / cakephp4 / datetime

输出

运行代码时,您将看到以下输出-

当前日期