Rumah  >  Artikel  >  pembangunan bahagian belakang  >  Tarikh dan Masa CakePHP

Tarikh dan Masa CakePHP

WBOY
WBOYasal
2024-09-10 17:27:07559semak imbas

Untuk bekerja dengan tarikh dan masa dalam cakephp4, kami akan menggunakan kelas FrozenTime yang tersedia.

Untuk bekerja dengan tarikh dan masa, masukkan kelas dalam pengawal anda

use Cake\I18n\FrozenTime;

Biar kami bekerja, pada contoh dan tarikh serta masa paparan, menggunakan kelas FrozenTime.

Contoh

Buat Perubahan dalam fail config/routes.php seperti yang ditunjukkan dalam program berikut.

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

Buat fail DatesController.php di src/Controller/DatesController.php. Salin kod berikut dalam fail pengawal. Abaikan jika sudah dibuat.

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

Buat direktori Tarikh di src/Template dan di bawah direktori itu cipta fail View yang dipanggil index.php. Salin kod berikut dalam fail itu.

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

Laksanakan contoh di atas dengan melawati URL berikut −

http://localhost/cakephp4/datetime

Output

Apabila anda menjalankan kod, anda akan melihat output berikut −

Current Date

Atas ialah kandungan terperinci Tarikh dan Masa CakePHP. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!

Kenyataan:
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Artikel sebelumnya:Pengesah Mencipta CakePHPArtikel seterusnya:Pengesah Mencipta CakePHP