Q - Joomla 5 - How to solve the bug of strftime() is deprecated in Joomla 5 custom component ?

Moderator: mindphp

Raja Pdl
PHP VIP Members
PHP VIP Members
โพสต์: 2241
ลงทะเบียนเมื่อ: 27/05/2024 9:50 am

Q - Joomla 5 - How to solve the bug of strftime() is deprecated in Joomla 5 custom component ?

โพสต์ที่ยังไม่ได้อ่าน โดย Raja Pdl »

In my custom component of Joomla 5,

In models/forms/event.xml, It shows the start date, end date input boxes, but above the input boxes, it shows

โค้ด: เลือกทั้งหมด

Deprecated: Function strftime() is deprecated in C:\xampp\htdocs\joomla5\libraries\src\Form\Field\CalendarField.php on line 321

โค้ด: เลือกทั้งหมด

<?xml version="1.0" encoding="utf-8"?>
<form>
        <fieldset name="event">
            <field
                name="id"
                type="hidden"
                label="JGLOBAL_FIELD_ID"
                description="JGLOBAL_FIELD_ID_DESC"
            />
            <field
                name="event_start_date"
                type="calendar"
                label="START_DATE"
                hint="ENTER_EVENT_START_DATE"
                required="true"
                showtime="true"
                timeformat="12"
                format="%Y-%m-%d %H:%M:%S"
                />
            <field
                name="event_end_date"
                type="calendar"
                hint='ENTER_EVENT_END_DATE'
                label="END_DATE"
                required="true"
                showtime="true"
                timeformat="12"
                format="%Y-%m-%d %H:%M:%S"
                />
        </fieldset>
</form>
When I removed the "%" sign, calendar is not clickable to select date.

How to solve it ?
MindPHP Assistant

Re: Q - Joomla 5 - How to solve the bug of strftime() is deprecated in Joomla 5 custom component ?

โพสต์ที่ยังไม่ได้อ่าน โดย MindPHP Assistant »

The deprecation warning you're seeing is because `strftime()` is deprecated in PHP 8.1 and will be removed in future versions. Joomla core should ideally update its usage of deprecated functions, but until then, you can work around it by updating your code.

The issue with the calendar not being clickable when you remove the "%" sign is because the format specifier for date and time in Joomla's calendar field requires a proper format string. The "%" sign is part of the `strftime` function format, and removing it invalidates the format string.

Here are a few steps to potentially solve the issue:

1. **Update Joomla Core Files (Temporary Fix):** You can patch the Joomla core file to use `date()` function instead of `strftime()`. Find the `CalendarField.php` file in `libraries\src\Form\Field` and replace the `strftime()` with an equivalent `date()` function. This is a temporary fix and not recommended for production as any update to Joomla might override this.

โค้ด: เลือกทั้งหมด

php
    // Replace this line
    $date = strftime($this->format, $value);

    // With this line
    $date = date($this->format, $value);
    
2. **Fix Your XML Form Field Definition:** Ensure that the format string is valid when using the `calendar` field type. Instead of removing the "%", use a valid PHP date format.

โค้ด: เลือกทั้งหมด

xml
    <field name="event_start_date"
           type="calendar"
           label="START_DATE"
           hint="ENTER_EVENT_START_DATE"
           required="true"
           showtime="true"
           timeformat="12"
           format="Y-m-d H:i:s"
    />
    <field name="event_end_date"
           type="calendar"
           label="END_DATE"
           hint="ENTER_EVENT_END_DATE"
           required="true"
           showtime="true"
           timeformat="12"
           format="Y-m-d H:i:s"
    />
    
3. **Custom Calendar Field Type:** Create a custom field type that extends the core `CalendarField` and overrides the method using `strftime` to use `date` instead. This way, you can use your custom field instead of the core one without affecting Joomla updates.

- Create a new file in your component's `fields` directory, for example `components/com_yourcomponent/models/fields/customcalendar.php`.

โค้ด: เลือกทั้งหมด

php
    <?php

    defined('JPATH_BASE') or die;

    use Joomla\CMS\Form\FormHelper;
    use Joomla\CMS\Form\Field\CalendarField;

    FormHelper::loadFieldClass('calendar');

    class JFormFieldCustomCalendar extends CalendarField
    {
        protected function getInput()
        {
            // Use parent method but swap out strftime for date
            $this->format = str_replace('%', '', $this->format); // Remove % if needed
            $this->format = 'Y-m-d H:i:s'; // Use PHP date format
            $value = $this->value ? date($this->format, strtotime($this->value)) : '';
            return parent::getInput();
        }
    }

    
- Use your custom field in the XML:

โค้ด: เลือกทั้งหมด

xml
    <field name="event_start_date"
           type="customcalendar"
           label="START_DATE"
           hint="ENTER_EVENT_START_DATE"
           required="true"
           showtime="true"
           timeformat="12"
           format="Y-m-d H:i:s"
    />
    <field name="event_end_date"
           type="customcalendar"
           label="END_DATE"
           hint="ENTER_EVENT_END_DATE"
           required="true"
           showtime="true"
           timeformat="12"
           format="Y-m-d H:i:s"
    />
    
By creating a custom field type, you ensure that your changes won't be overwritten by Joomla updates and you can handle any specific logic you need.
Raja Pdl
PHP VIP Members
PHP VIP Members
โพสต์: 2241
ลงทะเบียนเมื่อ: 27/05/2024 9:50 am

Re: Q - Joomla 5 - How to solve the bug of strftime() is deprecated in Joomla 5 custom component ?

โพสต์ที่ยังไม่ได้อ่าน โดย Raja Pdl »

MindPHP Assistant เขียน: 09/08/2024 4:21 pm The deprecation warning you're seeing is because `strftime()` is deprecated in PHP 8.1 and will be removed in future versions. Joomla core should ideally update its usage of deprecated functions, but until then, you can work around it by updating your code.

The issue with the calendar not being clickable when you remove the "%" sign is because the format specifier for date and time in Joomla's calendar field requires a proper format string. The "%" sign is part of the `strftime` function format, and removing it invalidates the format string.

Here are a few steps to potentially solve the issue:

1. **Update Joomla Core Files (Temporary Fix):** You can patch the Joomla core file to use `date()` function instead of `strftime()`. Find the `CalendarField.php` file in `libraries\src\Form\Field` and replace the `strftime()` with an equivalent `date()` function. This is a temporary fix and not recommended for production as any update to Joomla might override this.

โค้ด: เลือกทั้งหมด

php
    // Replace this line
    $date = strftime($this->format, $value);

    // With this line
    $date = date($this->format, $value);
    
2. **Fix Your XML Form Field Definition:** Ensure that the format string is valid when using the `calendar` field type. Instead of removing the "%", use a valid PHP date format.

โค้ด: เลือกทั้งหมด

xml
    <field name="event_start_date"
           type="calendar"
           label="START_DATE"
           hint="ENTER_EVENT_START_DATE"
           required="true"
           showtime="true"
           timeformat="12"
           format="Y-m-d H:i:s"
    />
    <field name="event_end_date"
           type="calendar"
           label="END_DATE"
           hint="ENTER_EVENT_END_DATE"
           required="true"
           showtime="true"
           timeformat="12"
           format="Y-m-d H:i:s"
    />
    
3. **Custom Calendar Field Type:** Create a custom field type that extends the core `CalendarField` and overrides the method using `strftime` to use `date` instead. This way, you can use your custom field instead of the core one without affecting Joomla updates.

- Create a new file in your component's `fields` directory, for example `components/com_yourcomponent/models/fields/customcalendar.php`.

โค้ด: เลือกทั้งหมด

php
    <?php

    defined('JPATH_BASE') or die;

    use Joomla\CMS\Form\FormHelper;
    use Joomla\CMS\Form\Field\CalendarField;

    FormHelper::loadFieldClass('calendar');

    class JFormFieldCustomCalendar extends CalendarField
    {
        protected function getInput()
        {
            // Use parent method but swap out strftime for date
            $this->format = str_replace('%', '', $this->format); // Remove % if needed
            $this->format = 'Y-m-d H:i:s'; // Use PHP date format
            $value = $this->value ? date($this->format, strtotime($this->value)) : '';
            return parent::getInput();
        }
    }

    
- Use your custom field in the XML:

โค้ด: เลือกทั้งหมด

xml
    <field name="event_start_date"
           type="customcalendar"
           label="START_DATE"
           hint="ENTER_EVENT_START_DATE"
           required="true"
           showtime="true"
           timeformat="12"
           format="Y-m-d H:i:s"
    />
    <field name="event_end_date"
           type="customcalendar"
           label="END_DATE"
           hint="ENTER_EVENT_END_DATE"
           required="true"
           showtime="true"
           timeformat="12"
           format="Y-m-d H:i:s"
    />
    
By creating a custom field type, you ensure that your changes won't be overwritten by Joomla updates and you can handle any specific logic you need.

Not working, I create new field file but the error is still showing.
And if I removed '%' signs from xml file , the calendar is not clickable,

โค้ด: เลือกทั้งหมด

<?php

defined('JPATH_BASE') or die;

use Joomla\CMS\Form\FormHelper;
use Joomla\CMS\Form\Field\CalendarField;

FormHelper::loadFieldClass('calendar');

class JFormFieldCustomCalendar extends CalendarField
{
    protected function getInput()
    {
        $this->format = str_replace('%', '', $this->format); 
        $this->format = 'Y-m-d H:i:s';
        $value = $this->value ? date($this->format, strtotime($this->value)) : '';
        return parent::getInput();
    }
}

โค้ด: เลือกทั้งหมด

<?xml version="1.0" encoding="utf-8"?>
<form>
        <fieldset name="event">
            <field
                name="id"
                type="hidden"
                label="JGLOBAL_FIELD_ID"
                description="JGLOBAL_FIELD_ID_DESC"
            />
            <field
                name="event_start_date"
                type="customcalendar"
                label="START_DATE"
                hint="ENTER_EVENT_START_DATE"
                required="true"
                showtime="true"
                timeformat="12"
                format="Y-m-d H:M:S"
                />
            <field
                name="event_end_date"
                type="customcalendar"
                hint='ENTER_EVENT_END_DATE'
                label="END_DATE"
                required="true"
                showtime="true"
                timeformat="12"
                format="%Y-%m-%d %H:%M:%S"
                />
        </fieldset>
</form>
MindPHP Assistant

Re: Q - Joomla 5 - How to solve the bug of strftime() is deprecated in Joomla 5 custom component ?

โพสต์ที่ยังไม่ได้อ่าน โดย MindPHP Assistant »

It looks like the custom calendar field type isn't overriding the format properly, and the XML format strings are inconsistent. Let's troubleshoot and refine your approach.

### Step-by-Step Solution

#### 1. Ensure Correct Custom Field Class

Make sure your custom field class is correctly loading and overriding the methods.

โค้ด: เลือกทั้งหมด

php
<?php

defined('JPATH_BASE') or die;

use Joomla\CMS\Form\FormHelper;
use Joomla\CMS\Form\Field\CalendarField;

FormHelper::loadFieldClass('calendar');

class JFormFieldCustomCalendar extends CalendarField
{
    protected function getInput()
    {
        // Ensure the format is correctly handled
        $this->format = str_replace('%', '', $this->format); 
        $this->format = $this->translateFormat($this->format);
        $value = $this->value ? date($this->format, strtotime($this->value)) : '';
        return parent::getInput();
    }

    private function translateFormat($format)
    {
        // Translate strftime format to date() format
        $replacements = [
            '%Y' => 'Y',
            '%m' => 'm',
            '%d' => 'd',
            '%H' => 'H',
            '%M' => 'i',
            '%S' => 's',
        ];
        return strtr($format, $replacements);
    }
}
#### 2. Update XML Form File

Make sure the format specified in the XML file is consistent and correctly uses PHP's date format. Remove the `%` if you want to follow PHP's format.

โค้ด: เลือกทั้งหมด

xml
<?xml version="1.0" encoding="utf-8"?>
<form>
    <fieldset name="event">
        <field
            name="id"
            type="hidden"
            label="JGLOBAL_FIELD_ID"
            description="JGLOBAL_FIELD_ID_DESC"
        />
        <field
            name="event_start_date"
            type="customcalendar"
            label="START_DATE"
            hint="ENTER_EVENT_START_DATE"
            required="true"
            showtime="true"
            timeformat="12"
            format="Y-m-d H:i:s" <!-- Correct format -->
        />
        <field
            name="event_end_date"
            type="customcalendar"
            hint="ENTER_EVENT_END_DATE"
            label="END_DATE"
            required="true"
            showtime="true"
            timeformat="12"
            format="Y-m-d H:i:s" <!-- Correct format -->
        />
    </fieldset>
</form>
### Troubleshooting

1. **Clear Cache**: Ensure Joomla's cache is cleared to reflect the changes.
2. **Validate Path**: Check if the custom field file is correctly placed in the expected directory.
3. **Check Overrides**: Ensure there are no other overrides or plugins affecting the `calendar` field type.
4. **Debugging**: Add logging or debugging output in the custom class to ensure it's being used.

### Additional Debugging

Add a debug statement to confirm the custom field is loaded.

โค้ด: เลือกทั้งหมด

php
protected function getInput()
{
    // Debug statement to verify
    error_log('Custom Calendar Field Loaded');
    $this->format = str_replace('%', '', $this->format);
    $this->format = $this->translateFormat($this->format);
    $value = $this->value ? date($this->format, strtotime($this->value)) : '';
    return parent::getInput();
}
Check your PHP error logs to see if the message appears. If not, there's an issue with how the custom field is being loaded or defined.

By following these steps, you should be able to properly override the deprecated `strftime` function and ensure your calendar fields are clickable and functional.
Raja Pdl
PHP VIP Members
PHP VIP Members
โพสต์: 2241
ลงทะเบียนเมื่อ: 27/05/2024 9:50 am

Re: Q - Joomla 5 - How to solve the bug of strftime() is deprecated in Joomla 5 custom component ?

โพสต์ที่ยังไม่ได้อ่าน โดย Raja Pdl »

MindPHP Assistant เขียน: 09/08/2024 4:35 pm It looks like the custom calendar field type isn't overriding the format properly, and the XML format strings are inconsistent. Let's troubleshoot and refine your approach.

### Step-by-Step Solution

#### 1. Ensure Correct Custom Field Class

Make sure your custom field class is correctly loading and overriding the methods.

โค้ด: เลือกทั้งหมด

php
<?php

defined('JPATH_BASE') or die;

use Joomla\CMS\Form\FormHelper;
use Joomla\CMS\Form\Field\CalendarField;

FormHelper::loadFieldClass('calendar');

class JFormFieldCustomCalendar extends CalendarField
{
    protected function getInput()
    {
        // Ensure the format is correctly handled
        $this->format = str_replace('%', '', $this->format); 
        $this->format = $this->translateFormat($this->format);
        $value = $this->value ? date($this->format, strtotime($this->value)) : '';
        return parent::getInput();
    }

    private function translateFormat($format)
    {
        // Translate strftime format to date() format
        $replacements = [
            '%Y' => 'Y',
            '%m' => 'm',
            '%d' => 'd',
            '%H' => 'H',
            '%M' => 'i',
            '%S' => 's',
        ];
        return strtr($format, $replacements);
    }
}
#### 2. Update XML Form File

Make sure the format specified in the XML file is consistent and correctly uses PHP's date format. Remove the `%` if you want to follow PHP's format.

โค้ด: เลือกทั้งหมด

xml
<?xml version="1.0" encoding="utf-8"?>
<form>
    <fieldset name="event">
        <field
            name="id"
            type="hidden"
            label="JGLOBAL_FIELD_ID"
            description="JGLOBAL_FIELD_ID_DESC"
        />
        <field
            name="event_start_date"
            type="customcalendar"
            label="START_DATE"
            hint="ENTER_EVENT_START_DATE"
            required="true"
            showtime="true"
            timeformat="12"
            format="Y-m-d H:i:s" <!-- Correct format -->
        />
        <field
            name="event_end_date"
            type="customcalendar"
            hint="ENTER_EVENT_END_DATE"
            label="END_DATE"
            required="true"
            showtime="true"
            timeformat="12"
            format="Y-m-d H:i:s" <!-- Correct format -->
        />
    </fieldset>
</form>
### Troubleshooting

1. **Clear Cache**: Ensure Joomla's cache is cleared to reflect the changes.
2. **Validate Path**: Check if the custom field file is correctly placed in the expected directory.
3. **Check Overrides**: Ensure there are no other overrides or plugins affecting the `calendar` field type.
4. **Debugging**: Add logging or debugging output in the custom class to ensure it's being used.

### Additional Debugging

Add a debug statement to confirm the custom field is loaded.

โค้ด: เลือกทั้งหมด

php
protected function getInput()
{
    // Debug statement to verify
    error_log('Custom Calendar Field Loaded');
    $this->format = str_replace('%', '', $this->format);
    $this->format = $this->translateFormat($this->format);
    $value = $this->value ? date($this->format, strtotime($this->value)) : '';
    return parent::getInput();
}
Check your PHP error logs to see if the message appears. If not, there's an issue with how the custom field is being loaded or defined.

By following these steps, you should be able to properly override the deprecated `strftime` function and ensure your calendar fields are clickable and functional.
calendar is not clickable and can't select date,
calendar is clickable only I added "%",
I want to solve the error of

โค้ด: เลือกทั้งหมด

Deprecated: Function strftime() is deprecated in C:\xampp\htdocs\joomla5\libraries\src\Form\Field\CalendarField.php on line 321
MindPHP Assistant

Re: Q - Joomla 5 - How to solve the bug of strftime() is deprecated in Joomla 5 custom component ?

โพสต์ที่ยังไม่ได้อ่าน โดย MindPHP Assistant »

The issue you're facing stems from the deprecation of the `strftime()` function, which you are trying to replace with PHP's `date()` function. The custom calendar field you are creating should override the necessary parts to avoid this deprecation warning. Here’s a step-by-step approach to resolve the issue:

1. **Create a Custom Calendar Field Class**:
This class will override the Joomla `CalendarField` class where the `strftime` function is being used.

2. **Update XML Form File**:
Ensure the date format in the XML file uses the PHP `date()` format.

3. **Debugging and Troubleshooting**:
Add debugging statements to ensure your custom field class is correctly loaded and used.

Here's the refined approach:

### Step 1: Create a Custom Calendar Field Class

Create your custom calendar field class in the appropriate location, typically under `administrator/components/com_yourcomponent/models/fields/`.

#### CustomCalendarField.php

โค้ด: เลือกทั้งหมด

php
<?php

defined('JPATH_BASE') or die;

use Joomla\CMS\Form\FormHelper;
use Joomla\CMS\Form\Field\CalendarField;

FormHelper::loadFieldClass('calendar');

class JFormFieldCustomCalendar extends CalendarField
{
    protected function getInput()
    {
        // Debug statement to verify the custom field is loaded
        error_log('Custom Calendar Field Loaded', 0);

        // Override the format to remove '%' and translate it
        $this->format = str_replace('%', '', $this->format);
        $this->format = $this->translateFormat($this->format);
        
        // Format the value if it exists
        $value = $this->value ? date($this->format, strtotime($this->value)) : '';
        
        // Set the formatted value
        $this->value = $value;

        // Call the parent getInput method
        return parent::getInput();
    }

    private function translateFormat($format)
    {
        // Translate strftime format to date() format
        $replacements = [
            'Y' => 'Y',
            'm' => 'm',
            'd' => 'd',
            'H' => 'H',
            'M' => 'i',
            'S' => 's',
        ];
        return strtr($format, $replacements);
    }
}
### Step 2: Update Your XML Form File

Ensure your format in the XML file uses PHP’s `date()` format.

#### form.xml

โค้ด: เลือกทั้งหมด

xml
<?xml version="1.0" encoding="utf-8"?>
<form>
    <fieldset name="event">
        <field
            name="id"
            type="hidden"
            label="JGLOBAL_FIELD_ID"
            description="JGLOBAL_FIELD_ID_DESC"
        />
        <field
            name="event_start_date"
            type="customcalendar"
            label="START_DATE"
            hint="ENTER_EVENT_START_DATE"
            required="true"
            showtime="true"
            timeformat="12"
            format="Y-m-d H:i:s" <!-- Correct format -->
        />
        <field
            name="event_end_date"
            type="customcalendar"
            hint="ENTER_EVENT_END_DATE"
            label="END_DATE"
            required="true"
            showtime="true"
            timeformat="12"
            format="Y-m-d H:i:s" <!-- Correct format -->
        />
    </fieldset>
</form>
### Step 3: Clear Cache and Validate Path

1. **Clear Cache**: Ensure Joomla's cache is cleared to reflect the changes.
2. **Validate Path**: Check if the custom field file is correctly placed in the expected directory.
3. **Check Overrides**: Ensure there are no other overrides or plugins affecting the `calendar` field type.
4. **Check PHP Error Logs**: Add a debug statement to confirm the custom field is loaded.

### Additional Debugging

Check your PHP error logs to see if the debug message appears. If the custom field is not loaded, there may be an issue with how the custom field class is defined or included.

By following these steps, you should be able to override the deprecated `strftime` function and ensure your calendar fields are clickable and functional without any deprecation warnings.
Raja Pdl
PHP VIP Members
PHP VIP Members
โพสต์: 2241
ลงทะเบียนเมื่อ: 27/05/2024 9:50 am

Re: Q - Joomla 5 - How to solve the bug of strftime() is deprecated in Joomla 5 custom component ?

โพสต์ที่ยังไม่ได้อ่าน โดย Raja Pdl »

MindPHP Assistant เขียน: 09/08/2024 4:39 pm The issue you're facing stems from the deprecation of the `strftime()` function, which you are trying to replace with PHP's `date()` function. The custom calendar field you are creating should override the necessary parts to avoid this deprecation warning. Here’s a step-by-step approach to resolve the issue:

1. **Create a Custom Calendar Field Class**:
This class will override the Joomla `CalendarField` class where the `strftime` function is being used.

2. **Update XML Form File**:
Ensure the date format in the XML file uses the PHP `date()` format.

3. **Debugging and Troubleshooting**:
Add debugging statements to ensure your custom field class is correctly loaded and used.

Here's the refined approach:

### Step 1: Create a Custom Calendar Field Class

Create your custom calendar field class in the appropriate location, typically under `administrator/components/com_yourcomponent/models/fields/`.

#### CustomCalendarField.php

โค้ด: เลือกทั้งหมด

php
<?php

defined('JPATH_BASE') or die;

use Joomla\CMS\Form\FormHelper;
use Joomla\CMS\Form\Field\CalendarField;

FormHelper::loadFieldClass('calendar');

class JFormFieldCustomCalendar extends CalendarField
{
    protected function getInput()
    {
        // Debug statement to verify the custom field is loaded
        error_log('Custom Calendar Field Loaded', 0);

        // Override the format to remove '%' and translate it
        $this->format = str_replace('%', '', $this->format);
        $this->format = $this->translateFormat($this->format);
        
        // Format the value if it exists
        $value = $this->value ? date($this->format, strtotime($this->value)) : '';
        
        // Set the formatted value
        $this->value = $value;

        // Call the parent getInput method
        return parent::getInput();
    }

    private function translateFormat($format)
    {
        // Translate strftime format to date() format
        $replacements = [
            'Y' => 'Y',
            'm' => 'm',
            'd' => 'd',
            'H' => 'H',
            'M' => 'i',
            'S' => 's',
        ];
        return strtr($format, $replacements);
    }
}
### Step 2: Update Your XML Form File

Ensure your format in the XML file uses PHP’s `date()` format.

#### form.xml

โค้ด: เลือกทั้งหมด

xml
<?xml version="1.0" encoding="utf-8"?>
<form>
    <fieldset name="event">
        <field
            name="id"
            type="hidden"
            label="JGLOBAL_FIELD_ID"
            description="JGLOBAL_FIELD_ID_DESC"
        />
        <field
            name="event_start_date"
            type="customcalendar"
            label="START_DATE"
            hint="ENTER_EVENT_START_DATE"
            required="true"
            showtime="true"
            timeformat="12"
            format="Y-m-d H:i:s" <!-- Correct format -->
        />
        <field
            name="event_end_date"
            type="customcalendar"
            hint="ENTER_EVENT_END_DATE"
            label="END_DATE"
            required="true"
            showtime="true"
            timeformat="12"
            format="Y-m-d H:i:s" <!-- Correct format -->
        />
    </fieldset>
</form>
### Step 3: Clear Cache and Validate Path

1. **Clear Cache**: Ensure Joomla's cache is cleared to reflect the changes.
2. **Validate Path**: Check if the custom field file is correctly placed in the expected directory.
3. **Check Overrides**: Ensure there are no other overrides or plugins affecting the `calendar` field type.
4. **Check PHP Error Logs**: Add a debug statement to confirm the custom field is loaded.

### Additional Debugging

Check your PHP error logs to see if the debug message appears. If the custom field is not loaded, there may be an issue with how the custom field class is defined or included.

By following these steps, you should be able to override the deprecated `strftime` function and ensure your calendar fields are clickable and functional without any deprecation warnings.
The error is solved only when I removed strftime() from
'C:\xampp\htdocs\joomla5\libraries\src\Form\Field\CalendarField.php,

I changed , from

โค้ด: เลือกทั้งหมด

$this->value = strftime($this->format, strtotime($this->value));
to

โค้ด: เลือกทั้งหมด

$this->value = $this->format;
But I can't change from other Joomla files, I have to change only from my custom component. How can I do?
MindPHP Assistant

Re: Q - Joomla 5 - How to solve the bug of strftime() is deprecated in Joomla 5 custom component ?

โพสต์ที่ยังไม่ได้อ่าน โดย MindPHP Assistant »

To resolve the deprecation issue without modifying Joomla core files, you need to ensure your custom calendar field class correctly overrides the core `CalendarField`. Here's a step-by-step guide to achieve this:

### Step 1: Create a Custom Calendar Field Class

Ensure your custom field overrides the core `CalendarField` correctly. You already have a good starting point but need to ensure the format handling is done properly.

#### CustomCalendarField.php

Create this file under `administrator/components/com_yourcomponent/models/fields/`:

โค้ด: เลือกทั้งหมด

php
<?php

defined('JPATH_BASE') or die;

use Joomla\CMS\Form\FormHelper;
use Joomla\CMS\Form\Field\CalendarField;

FormHelper::loadFieldClass('calendar');

class JFormFieldCustomCalendar extends CalendarField
{
    protected function getInput()
    {
        // Debug statement to verify the custom field is loaded
        error_log('Custom Calendar Field Loaded', 0);

        // Ensure the format is translated correctly
        $this->format = $this->translateFormat($this->format);
        
        // Format the value if it exists
        $value = $this->value ? date($this->format, strtotime($this->value)) : '';
        
        // Set the formatted value
        $this->value = $value;

        // Call the parent getInput method
        return parent::getInput();
    }

    private function translateFormat($format)
    {
        // Translate strftime format to date() format
        $replacements = [
            '%Y' => 'Y',
            '%m' => 'm',
            '%d' => 'd',
            '%H' => 'H',
            '%M' => 'i',
            '%S' => 's',
        ];
        return strtr($format, $replacements);
    }
}
### Step 2: Update Your XML Form File

Ensure that the custom field type is used in your form XML file:

#### form.xml

โค้ด: เลือกทั้งหมด

xml
<?xml version="1.0" encoding="utf-8"?>
<form>
    <fieldset name="event">
        <field
            name="id"
            type="hidden"
            label="JGLOBAL_FIELD_ID"
            description="JGLOBAL_FIELD_ID_DESC"
        />
        <field
            name="event_start_date"
            type="customcalendar"
            label="START_DATE"
            hint="ENTER_EVENT_START_DATE"
            required="true"
            showtime="true"
            timeformat="12"
            format="%Y-%m-%d %H:%M:%S" <!-- Use strftime format here for compatibility -->
        />
        <field
            name="event_end_date"
            type="customcalendar"
            hint="ENTER_EVENT_END_DATE"
            label="END_DATE"
            required="true"
            showtime="true"
            timeformat="12"
            format="%Y-%m-%d %H:%M:%S" <!-- Use strftime format here for compatibility -->
        />
    </fieldset>
</form>
### Step 3: Ensure Joomla Recognizes Your Custom Field

Clear Joomla's cache to ensure it recognizes your new custom field:

1. **Clear Cache**: Go to System → Clear Cache and clear all cache.
2. **Validate Path**: Make sure the custom field file is correctly placed in `administrator/components/com_yourcomponent/models/fields/`.
3. **Check Overrides**: Ensure no other overrides or plugins are affecting the `calendar` field type.

### Step 4: Debugging and Verification

Check PHP error logs to ensure that your custom field is loaded and see if the debug message appears:

โค้ด: เลือกทั้งหมด

bash
tail -f /path/to/your/php/error.log
### Summary

By following these steps, your custom calendar field should override the core `CalendarField` without modifying Joomla core files, thus eliminating deprecation warnings due to `strftime()`. This ensures your component is maintainable and compatible with future Joomla updates.
Raja Pdl
PHP VIP Members
PHP VIP Members
โพสต์: 2241
ลงทะเบียนเมื่อ: 27/05/2024 9:50 am

Re: Q - Joomla 5 - How to solve the bug of strftime() is deprecated in Joomla 5 custom component ?

โพสต์ที่ยังไม่ได้อ่าน โดย Raja Pdl »

I've solved this problem by changing (in CalendarField.php)

from

โค้ด: เลือกทั้งหมด

    $this->value = strftime($this->format, strtotime($this->value));
to

โค้ด: เลือกทั้งหมด

    $this->value = $this->format;
ตอบกลับโพส
  • Similar Topics
    ตอบกลับ
    แสดง
    โพสต์ล่าสุด

ผู้ใช้งานขณะนี้

สมาชิกกำลังดูบอร์ดนี้: ไม่มีสมาชิกใหม่ และบุคลทั่วไป 9