imgrooot Posted April 16, 2019 Share Posted April 16, 2019 Say I am running multiple contests at the same time. Each contest may have a different end date. One contest might run for a day, while the other one might run for 5 days. Below is the code done my way. I was wondering how you can improve it to make it more efficient? $current_date = date('Y-m-d H:i:s'); $find_contest = $db->prepare("SELECT days_count, date_started FROM contests WHERE status = :status"); $find_contest->bindValue(':status', 1); $find_contest->execute(); $result_contest = $find_contest->fetchAll(PDO::FETCH_ASSOC); if(count($result_contest) > 0) { foreach($result_contest as $row) { $days_count = $row['days_count']; // NUMBER OF DAYS THE CONTEST IS RUNNING $date_started = $row['date_started']; // START DATE FOR THE CONTEST $date_1 = DateTime::createFromFormat('Y-m-d H:i:s',$date_started); $date_1->modify('+1 day'); $end_date_1 = $date_1->format('Y-m-d H:i:s'); $date_2 = DateTime::createFromFormat('Y-m-d H:i:s',$date_started); $date_2->modify('+2 days'); $end_date_2 = $date_2->format('Y-m-d H:i:s'); $date_3 = DateTime::createFromFormat('Y-m-d H:i:s',$date_started); $date_3->modify('+3 days'); $end_date_3 = $date_3->format('Y-m-d H:i:s'); if($days_count == 1) { if($current_date > $end_date_1 AND $current_date < $end_date_2) { $day = 1; $end_date = $end_date_1; require 'run-contest.php'; } } else if($days_count == 2) { if($current_date > $end_date_1 AND $current_date < $end_date_2) { $day = 1; $end_date = $end_date_1; require 'run-contest.php'; } else if($current_date > $end_date_2 AND $current_date < $end_date_3) { $day = 2; $end_date = $end_date_2; require 'run-contest.php'; } else {} } else {} } } Quote Link to comment https://forums.phpfreaks.com/topic/308599-is-there-a-way-to-simplify-this-code/ Share on other sites More sharing options...
benanamen Posted April 16, 2019 Share Posted April 16, 2019 (edited) A minor comment. This line if(count($result_contest) > 0) Can simply be if($result_contest) The return value for fetchALL is either an array with results or an empty array, or false on failure. There is no need to count anything. The default truthy check of the if is all you need. Edited April 16, 2019 by benanamen Quote Link to comment https://forums.phpfreaks.com/topic/308599-is-there-a-way-to-simplify-this-code/#findComment-1566127 Share on other sites More sharing options...
Barand Posted April 16, 2019 Share Posted April 16, 2019 Something like this, perhaps TEST DATA +----+-----------+--------------+------------+ | id | name | date_started | days_count | +----+-----------+--------------+------------+ | 1 | Contest 1 | 2019-04-16 | 1 | | 2 | Contest 2 | 2019-04-15 | 3 | | 3 | Contest 3 | 2019-04-14 | 5 | | 4 | Contest 4 | 2019-04-14 | 2 | | 5 | Contest 5 | 2019-04-15 | 1 | +----+-----------+--------------+------------+ QUERY SELECT name , date_started as startdate , date_started + INTERVAL days_count-1 DAY as enddate , datediff(curdate(), date_started) + 1 as daynum FROM contests WHERE curdate() BETWEEN date_started AND date_started + INTERVAL days_count-1 DAY ORDER BY date_started; RESULTS +-----------+------------+------------+--------+ | name | startdate | enddate | daynum | +-----------+------------+------------+--------+ | Contest 3 | 2019-04-14 | 2019-04-18 | 3 | | Contest 2 | 2019-04-15 | 2019-04-17 | 2 | | Contest 1 | 2019-04-16 | 2019-04-16 | 1 | +-----------+------------+------------+--------+ Quote Link to comment https://forums.phpfreaks.com/topic/308599-is-there-a-way-to-simplify-this-code/#findComment-1566129 Share on other sites More sharing options...
imgrooot Posted April 16, 2019 Author Share Posted April 16, 2019 15 hours ago, Barand said: Something like this, perhaps TEST DATA +----+-----------+--------------+------------+ | id | name | date_started | days_count | +----+-----------+--------------+------------+ | 1 | Contest 1 | 2019-04-16 | 1 | | 2 | Contest 2 | 2019-04-15 | 3 | | 3 | Contest 3 | 2019-04-14 | 5 | | 4 | Contest 4 | 2019-04-14 | 2 | | 5 | Contest 5 | 2019-04-15 | 1 | +----+-----------+--------------+------------+ QUERY SELECT name , date_started as startdate , date_started + INTERVAL days_count-1 DAY as enddate , datediff(curdate(), date_started) + 1 as daynum FROM contests WHERE curdate() BETWEEN date_started AND date_started + INTERVAL days_count-1 DAY ORDER BY date_started; RESULTS +-----------+------------+------------+--------+ | name | startdate | enddate | daynum | +-----------+------------+------------+--------+ | Contest 3 | 2019-04-14 | 2019-04-18 | 3 | | Contest 2 | 2019-04-15 | 2019-04-17 | 2 | | Contest 1 | 2019-04-16 | 2019-04-16 | 1 | +-----------+------------+------------+--------+ Yes this is what I was looking for. I'll give it a shot. Thanks you. Quote Link to comment https://forums.phpfreaks.com/topic/308599-is-there-a-way-to-simplify-this-code/#findComment-1566147 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.