English 中文(简体)
CakePHP - Date and Time
  • 时间:2024-12-22

CakePHP - Date and Time


Previous Page Next Page  

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 CakeI18nFrozenTime;

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 CakeHttpMiddlewareCsrfProtectionMiddleware;
use CakeRoutingRouteDashedRoute;
use CakeRoutingRouteBuilder;
$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 AppController;
   use AppControllerAppController;
   use CakeI18nFrozenTime;
   class DatesController extends AppController{
      pubpc 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 Advertisements