0

I have a custom function in a controller file, the function basically is to be used for converting the time into a different custom format, for example, if the time is indicating the date of yesterday, it shows "1 day ago".

In any case, this is how I added the function in my controller file:

private function changeDate($datetime)
{
    $now = new DateTime;
    $ago = $datetime;
    $diff = $now->diff($ago);

    $diff->w = floor($diff->d / 7);
    $diff->d -= $diff->w * 7;

    $string = array(
        'y' => 'year',
        'm' => 'month',
        'w' => 'week',
        'd' => 'day',
        'h' => 'hour',
        'i' => 'minute',
        's' => 'second',
    );
    foreach ($string as $k => &$v) {
        if ($diff->$k) {
            $v = $diff->$k . ' ' . $v . ($diff->$k > 1 ? 's' : '');
        } else {
            unset($string[$k]);
        }
    }
    return $string ? implode(', ', $str,ing) . ' ago' : 'just now';    
}  

The function itself works fine, for example, I used it in some PHTML file and there was no issue, but when I use it in a controller PHP file, it returns an error.

I have checked the error log and this is the error:

Fatal error:  Uncaught Error: Class 'vendor\module\Controller\Customizer\DateTime' not found in /Users/folder/mag/app/code/vendor/module/Controller/Customizer/finalize.php:108

I am very new to Magento, and it is definitely a user error, but I am not sure what I am doing wrong, any help is greatly appreciated.

1
  • Please show your controller file here.. – Manish Goswami May 1 '18 at 11:45
0

As per your Error, "Vendor" comes first then "Module" in namespace.

So, change the code where you have included the class.

Class 'module\vendor\Controller\Customizer\DateTime'

Instead of this try to use

Class 'Vendor\Module\Controller\Customizer\DateTime'

Hope this will fix your error.

As per your below comment if it was only a typo mistake then it's fine, Then,edit this file app/bootstrap.php and remove # from below line.

ini_set('display_errors', 1);

It will show you the error and you can fix it, and you can also check log files in var/log/ folder.

3
  • That is not the issue, it was just a typo error on the question, I have edited the question, thanks for the heads up. – Bondsmith May 1 '18 at 7:42
  • Please disregard how the error is displayed, as I mentioned, I typed it in the question, and it is being edited since. The issue is not about the error and how it is displayed in the error log, but why this specific function returns the error. – Bondsmith May 1 '18 at 20:27
  • Keep in mind that the classnames are case-sensitive – Shawn Abramson May 2 '18 at 5:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.