首页  >  文章  >  后端开发  >  CakePHP 日期和时间

CakePHP 日期和时间

WBOY
WBOY原创
2024-09-10 17:27:07775浏览

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

To work with date and time, include the class in your controller

use Cake\I18n\FrozenTime;

Let us work, on an example and display date and time, using FrozenTime class.

Example

Make Changes in the config/routes.php file as shown in the following program.

config/routes.php

<?php
use Cake\Http\Middleware\CsrfProtectionMiddleware;
use Cake\Routing\Route\DashedRoute;
use Cake\Routing\RouteBuilder;
$routes->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();
});

Create a DatesController.php file at src/Controller/DatesController.php. Copy the following code in the controller file. Ignore if already created.

src/Controller/DatesController.php

<?php
   namespace App\Controller;
   use App\Controller\AppController;
   use Cake\I18n\FrozenTime;
   class DatesController extends AppController{
      public function index(){
         $time = FrozenTime::now();
         $now = FrozenTime::parse('now');
         $_now = $now->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);
      }
   }
?>

Create a directory Dates at src/Template and under that directory create a View file called index.php. Copy the following code in that file.

src/Template/Dates/index.php

<?php
   echo "The Current date and time is = ".$timenow;
   echo "<br/>";
   echo "Using nice format available = ".$nicetime;
   echo "<br/>";
   echo "Date and Time as per Hebrew Calender =" .$hebrewdate;
   echo "<br/>";
   echo "Date and Time as per Japanese Calender =" .$japanesedate;
   echo "<br/>";
   echo "Current Year = ".$current_year;
   echo "<br/>";
   echo "Current Month = ".$current_month;
   echo "<br/>";
   echo "Current Day = ".$current_day;
?>

Execute the above example by visiting the following URL −

http://localhost/cakephp4/datetime

Output

When you run the code, you will see the following output −

Current Date

以上是CakePHP 日期和时间的详细内容。更多信息请关注PHP中文网其他相关文章!

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn