2017-03-23 50 views
1

多年使用stackoverflow後的第一個問題。感謝所有這些年來的答案。如何在sonata_type_datetime_picker上設置時區

我遇到了sonata_type_datetime_picker問題。

我的php.ini時區設置爲歐洲/巴黎。

在我的奏鳴曲管理員中,我有一個表單來設置憑證的日期開始和日期結束。

  ->with('Conditions',['translation_domain' => 'SmallableAdminBundle']) 
      ->add('startAt', 'sonata_type_datetime_picker', [ 
       'label' => 'form.commercialvoucher.startAt', 
       'translation_domain' => 'SmallableAdminBundle', 
       'format' => 'dd/MM/yyyy HH:mm', 
       'attr' => [ 

       ], 
      ]) 
      ->add('endAt', 'sonata_type_datetime_picker', [ 
       'label' => 'form.commercialvoucher.endAt', 
       'translation_domain' => 'SmallableAdminBundle', 
       'format' => 'dd/MM/yyyy HH:mm', 
       'attr' => [ 

       ], 
      ]) 

當我提交表格時,似乎datetime是GMT時間,所以我基本上失去了一個小時。

例如:當我編輯我的優惠券時,更改日期並提交,這裏用10h00,我得到9:00。

CRUDController.php on line 362: 
CommercialVoucher {#241 ▼ 
     #id: 537287 
     #code: "TEST_time" 
     #name: "TEST_time" 
     #description: "TEST_time" 
     #validated: true 
     #startAt: DateTime {#2791 ▼ 
     +"date": "2014-12-22 09:02:00.000000" 
     +"timezone_type": 1 
     +"timezone": "+00:00" 
     } 
     #endAt: DateTime {#2794 ▼ 
     +"date": "2014-12-22 09:00:00.000000" 
     +"timezone_type": 1 
     +"timezone": "+00:00" 
     } 

當我對憑證做了其他更改,使日期保持不變,我得到正確的時區。

CRUDController.php on line 362: 
CommercialVoucher {#241 ▼ 
    #id: 537287 
    #code: "TEST_time" 
    #name: "TEST_time" 
    #description: "TEST_time" 
    #validated: true 
    #startAt: DateTime {#1397 ▼ 
    +"date": "2014-12-22 09:02:00.000000" 
    +"timezone_type": 3 
    +"timezone": "Europe/Paris" 
    } 
    #endAt: DateTime {#1398 ▼ 
    +"date": "2014-12-22 09:00:00.000000" 
    +"timezone_type": 3 
    +"timezone": "Europe/Paris" 
    } 

我試了很多在我的憑證管理員,使用選項格式,date_format。我也嘗試在sonata配置中設置時區,但目前爲止沒有任何工作。

+0

這可能是一個錯誤。你能否向項目提交bugreport? https://github.com/sonata-project/SonataAdminBundle –

回答

0

我嘗試了不同的替代解決方案。

我最好的選擇是用默認的symfony小部件替換奏鳴曲日期選擇器。

   ->add('startAt', 'datetime', [ 
       'date_widget'  => 'single_text', 
       'time_widget'  => 'single_text', 
       'label' => 'form.commercialvoucher.startAt', 
       'translation_domain' => 'SmallableAdminBundle', 
       'attr' => [ 

       ], 
      ]) 

而且我還在我的實體中初始化日期值,以便用戶最經常只能更改日期和月份。

$startAt = new \DateTime('now'); 
    $endAt = new \DateTime('now'); 

    $startAt->setTime(0, 0, 0); 
    $this->setStartAt($startAt); 
    $endAt->setTime(23, 59, 59); 
    $this->setEndAt($endAt); 

DateTime widget on the left

Date and Time picker