简体   繁体   中英

when getting DatePeriod Dates and looping through them it is giving me this error “Cannot use object of type DateTime as array”

first, Thank for allowing me to ask this question i have really weird problem that i have been trying to figure out the source of the error in my script for about three hours and finally i reached something , now i know the code that is the source of the problem but don't know how to solve the problem , the code is giving me this error "Cannot use object of type DateTime as array" i have this code

$academicYear = \academic_year::where('isDefault', 1)->first();

$firstTerm = new \DatePeriod(
    new \DateTime($academicYear->firstterm_start_date),
    new \DateInterval('P1M'),
    new \DateTime($academicYear->firstterm_end_date)
);

$secondTerm = new \DatePeriod(
    new \DateTime($academicYear->secondterm_start_date),
    new \DateInterval('P1M'),
    new \DateTime($academicYear->secondterm_end_date)
);

$monthlyDates = [];
foreach ($firstTerm as $key => $value) {
    $monthlyDates[] = ['date' => strtotime((string)$value->format('Y-m-d')), 'due' => strtotime((string)$value->format('Y-m-d')) ];
}

foreach ($secondTerm as $key => $value) {
    $monthlyDates[] = ['date' => strtotime((string)$value->format('Y-m-d')), 'due' => strtotime((string)$value->format('Y-m-d'))];
}

all i am trying to do here is just get the dates between two dates for an interval of time of one month and change the structure of the dates to make it date and due in an array and get the unix timestamp of these dates generated from this DatePeriod Class

so the weird thing here is that when i try the code this way it give me this error as the result of this code is going into another foreach loop not to make it long question when i try this code it give me "Cannot use object of type DateTime as array" and when i get the result of die and dump dd($monthlyDates) and put it instead of the code that is getting the dates dynamically the script works fine and here is the 结果 of dumping the value of $monthlyDates

and the code below is the array from the dd($monthlyDates) that i used instead of the \\DatePeriod result

$monthlyDates = [
    0 => [
        'date'=> 1569888000,
        'due' => 1569888000 
    ],
    1 => [
        'date' => 1572566400,
        'due' => 1572566400
    ],
    2 => [
        'date' => 1575158400,
        'due' => 1575158400
    ],
    3 => [
        'date' => 1580515200,
        'due' => 1580515200
    ],
    4 => [
        'date' => 1583020800,
        'due' => 1583020800
    ],
    5 => [
        'date' => 1585699200,
        'due' => 1585699200
    ]
];

so when i use this array above instead of the code of \\DatePeriod , i get the expected result of the code and dont know what makes this code works and the first one giving me this error

I just solved my problem, i was wondering why it isn't giving me the expected result although i found the expected result using die and dump dd() function from laravel So i thought how can i reach this result so i found out that dd() function returns a value so i created a method in the same class and returned the $monthlyDates i need for my app and now it works like a charm

protected function getMonthlyDates($firstTerm , $secondTerm)
{
    $monthlyDates = [];
    foreach ($firstTerm as  $value) {
        $monthlyDates[] = ['date' => strtotime($value->format('Y-m-d')), 'due' => strtotime($value->format('Y-m-d'))];
    }

    foreach ($secondTerm as  $value) {
        $monthlyDates[] = ['date' => strtotime($value->format('Y-m-d')), 'due' => strtotime($value->format('Y-m-d'))];
    }
    return $monthlyDates;
}

hope this will help someone

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM