phpfuelphp

Call to a member function format() on boolean using PHP and FuelPHP MVC Framework


I am aware that this is a common error and has more than likely been answered, but the issue I am having is when the function is executed it's returning the $down_time variable as null when a new website becomes offline on this line $diff_in_seconds = $now_time->getTimestamp() - $down_time->getTimestamp(); however when I run the function again in my terminal the error then goes. Basically it's returning $down_time variable as null before it is updated into the database, which I'm confused about since the update query is executed first. I would really appreciate the help and guidance thanks.

    public static function run()
    {
        $dept_emails = array(
            19 => 'test@email.co.uk'
        );

        $active_clients = Client::get_active_clients();

        foreach ($active_clients as $client) {
            $websites = $client->websites;

            $services = $client->services;

            foreach ($websites as $website) {
                $website_url = $website->url;
                $website_url_parse = parse_url($website_url);
                $new_website_url = "https://" . $website_url;
                $website_status = Client::get_website_status($new_website_url);

                if(array_key_exists('host', $website_url_parse)) {
                    $website_url = $website_url_parse['host'];
                }

                if($website_status == false) {
                    $new_website_url = "http://" . $website_url;
                    $website_status = Client::get_website_status($new_website_url);
                }


                $now = date('Y-m-d H:i:s');
                $now_time = DateTime::createFromFormat('Y-m-d H:i:s', $now);

                $down_time = DateTime::createFromFormat('Y-m-d H:i:s', $website->down_at);


                $result = DB::update('clients_websites')
                    ->set(array(
                        'last_checked' => $now
                    ))
                    ->where('id', '=', $website->id)
                    ->execute();

                if ($website_status) {
                    // Website is back online, send email
                    if($website->down_at != null && $website_status == true){


                        $diff_in_seconds = $now_time->getTimestamp() - $down_time->getTimestamp();
                        // Website has been down for more than 5 minutes
                        if($diff_in_seconds >= 300){

                            Cli::write("Emailing: " . $website->url . " is back online");

                            $result = DB::update('clients_websites')
                                ->set(array(
                                    'down_at' => null,
                                    'up_at' => date('Y-m-d H:i:s'),
                                ))
                                ->where('id', '=', $website->id)
                                ->execute();

                            $notify_emails = array();

                            foreach ($services as $service)
                            {
                                $service_id = $service->service_id;
                                if (in_array($service_id, $dept_emails))
                                {
                                    $notify_emails[] = $dept_emails[$service_id];
                                }
                            }

                            $message = "Hi " . $website->url . " is back online.";
                            $email = Model_Mail::send_email($dept_emails, "" . $website->url . " is back online", $message);
                        }

                    } else {
                        // Website is online insert up time in to DB
                        Cli::write("Online: " . $website->url . " is online");

                        $result = DB::update('clients_websites')
                            ->set(array(
                                'down_at' => null,
                                'up_at' => date('Y-m-d H:i:s'),
                            ))
                            ->where('id', '=', $website->id)
                            ->execute();
                    }

                } else {
                    // Website is down insert down time in to DB
                    Cli::write("Offline: " . $website->url . " is offline");

                   if(is_null($website->down_at)){
                        $result = DB::update('clients_websites')
                            ->set(array(
                                'down_at' => date('Y-m-d H:i:s')
                            ))
                            ->where('id', '=', $website->id)
                            ->execute();

                            $query = DB::select('*')->from('clients_websites')->where('id', '=', $website->id)
                                ->and_where_open()
                                ->where('down_at', 'IS NOT', NULL)
                                ->and_where_close()
                                ->execute();

                        $down_time = DateTime::createFromFormat('Y-m-d H:i:s',  $query['down_at']);
                        $down_time_email = $down_time->format('d-m-Y H:i:s');
                        $diff_in_seconds = $now_time->getTimestamp() - $down_time->getTimestamp();

                        if($diff_in_seconds >= 300){

                            Cli::write("Emailing: " . $website->url . " is offline");

                            $notify_emails = array();

                            foreach ($services as $service)
                            {
                                $service_id = $service->service_id;
                                $active = $service->end_date;
                                if (in_array($service_id, $dept_emails ) && ($active == null || $active > $now))
                                {
                                    $notify_emails[] = $dept_emails[$service_id];
                                }
                            }

                            $message = "Hi " . $website->url . " has been down since " . $down_time_email . ".";
                            $email = Model_Mail::send_email($dept_emails, "" . $website->url . " is down", $message);

                            $result = DB::update('clients_websites')
                                ->set(array(
                                    'down_email_sent' => date('Y-m-d H:i:s')
                                ))
                                ->where('id', '=', $website->id)
                                ->execute();
                        }
                    }
                }
            }
        }
    }

Solution

  • Figured this out at the beginning of the website foreach I defined a new variable $time_now = date('Y-m-d H:i:s'); which was then used in the update for query for the down_at time and assigned to $down_time variable.

    $result = DB::update('clients_websites')
                                ->set(array(
                                    'down_at' => $time_now
                                ))
                                ->where('id', '=', $website->id)
                                ->execute();
                        }
    
                        $down_time = DateTime::createFromFormat('Y-m-d H:i:s', $time_now);