Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Liosva's avatar

php function in controller

Hi guys i have some problems with php function. I have script that looks like this:

$format = "Y-m-d";
$begin = new DateTime($startDate);
$end = new DateTime($endDate);
$interval = new DateInterval('P1M');
$dateRange = new DatePeriod($begin, $interval, $end);
$range = [];
foreach ($dateRange as $date) {
    $range[] = $date->format($format);
}

but then i put this part of code in controller i get error: FatalErrorException in dashboard_controller.php line 107: Class 'App\Http\Controllers\DateTime' not found

Thanks for help

0 likes
1 reply
xsmalbil@icloud.com's avatar
Level 12

you need to use the class.

<?php namespace App\Http\Controllers;

use DateTime;
use DatePeriod;
use DateIntercal;

class DashboardController extends Controller
{
    // your stuff
}

Please or to participate in this conversation.