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 => '[email protected]'
);
$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();
}
}
}
}
}
}
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);
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Find the answer in similar questions on our website.
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.
PHP (from the English Hypertext Preprocessor - hypertext preprocessor) is a scripting programming language for developing web applications. Supported by most hosting providers, it is one of the most popular tools for creating dynamic websites.
The PHP scripting language has gained wide popularity due to its processing speed, simplicity, cross-platform, functionality and distribution of source codes under its own license.
https://www.php.net/
FuelPHP is an MVC framework that was released in 2011 and is maintained by the developer community. He is known for his flexibility. It implements a special version of MVC - HMVC (Hierarchical Model-View-Controller - a hierarchical version of the model-view-controller architecture). HMVC, unlike MVC, encourages code reuse. FuelPHP also offers developers great extensibility, modularity, and good code organization. As a result, this framework allows programmers to save time and conserves system resources.
https://fuelphp.com/
Welcome to the Q&A site for web developers. Here you can ask a question about the problem you are facing and get answers from other experts. We have created a user-friendly interface so that you can quickly and free of charge ask a question about a web programming problem. We also invite other experts to join our community and help other members who ask questions. In addition, you can use our search for questions with a solution.
Ask about the real problem you are facing. Describe in detail what you are doing and what you want to achieve.
Our goal is to create a strong community in which everyone will support each other. If you find a question and know the answer to it, help others with your knowledge.