John Davidson

php - Timezone change after saving the content

0 comments
Message:


We need to display a default starttime on BE when adding a News (tx_news)


TCA :


    'starttime' => [
'exclude' => true,
'label' => 'LLL:EXT:frontend/Resources/Private/Language/locallang_ttc.xlf:starttime_formlabel',
'config' => [
'type' => 'input',
'renderType' => 'inputDateTime',
'size' => 16,
'eval' => 'datetime,int',
'default' => date('Y-m-d H:i:s'),
'behaviour' => [
'allowLanguageSynchronization' => true,
],
]
],

So, when I add a news the date is displayed properly :



But after saving the news, the time changes to :



We have already defined on php.ini date.timezone = Europe/London and on Install Tool [SYS][phpTimeZone] = Europe/London



My Answer:
To ensure that the timezone remains consistent throughout the process of adding and saving news content, you can explicitly set the timezone in your PHP code before retrieving or saving the date/time values.

You can use the following code snippet to set the timezone before retrieving the current date and time for the default value in your TCA configuration:

php
// Set the timezone to Europe/London
date_default_timezone_set('Europe/London');

// Get the current date and time in the specified timezone
$defaultStartTime = date('Y-m-d H:i:s');

// TCA configuration for 'starttime'
'starttime' => [
'exclude' => true,
'label' => 'LLL:EXT:frontend/Resources/Private/Language/locallang_ttc.xlf:starttime_formlabel',
'config' => [
'type' => 'input',
'renderType' => 'inputDateTime',
'size' => 16,
'eval' => 'datetime,int',
'default' => $defaultStartTime,
'behaviour' => [
'allowLanguageSynchronization' => true,
],
]
],


By explicitly setting the timezone before retrieving the current date and time, you can ensure that the default value displayed in the backend remains consistent with the specified timezone. This should prevent any unexpected changes in the displayed time after saving the news content.

Rate this post

4 of 5 based on 2477 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us