开发者

Comparing dates in codeigniter & mySQL

I am totally beside myself that I can't figure this out myself, and have read every article I can get my hands on but alas still nothing. So I'm hoping someone can help.

I have a table that holds Shows. It has four time fields show_start(HH:MM AM/PM), show_end(HH:MM AM/PM), show_start_actual(HH:MM:SS), show_end_actual(HH:MM:SS). Also I have 7 columns named for the days of the week (sunday, monday, etc). Days of the week are either 1 or 0 to annotate whether the show is on that certain day. The actual columns are holding a 24 hour time format, the other times are a varchar field.

I am trying to pull back the Show that is currently playing. The times are pretty basic (ie. 6:00 am - 10:00 am etc.) I have tried this in PHP and MySQL and nothing I am doing is working. I am sure there is an easy solution to this.

This is my current rendition that is comparing the times in PHP, but I think it would be more efficient if I can actually pull it back in mySQL.

Model

function on_air()
{
    $current_time = $this->convert_to_hour(date('g:i a',now()));
    $current_day_of_week = strtolower(date('l',now()));

    $mySQL = $this->db->query("SELECT * FROM on_air_now");

    if($mySQL->num_rows() > 0 ){
        $mySQL = $mySQL->result();  
        foreach($mySQL as $row){

            eval('$db_day_of_week = $row->'.$current_day_of_week.';');
            $show_start = $this->convert_to_hour($row->show_start);
            $show_end = $this->convert_to_hour($row->show_end);

            if($current_time > $show_start && $current_time < $show_end && $db_day_of_week == 1){
                // If true then go at the show tables to  get info...
            }

        }
    }else{
        return FALSE;
    }
}

function convert_to_hour($str){
    $temp_arr = explode(":",$str);
    $arr_cnt = count($temp_arr)-1;
    $hour = $temp_arr[0];
    $min = explode(" ",$temp_arr[$arr_cnt]);
    $ampm = $min[1];
    if($ampm == 'pm' || $ampm == 'PM'){
        if($hour !== '12'){
        $hour = $hour+12;
        }
    }else{
        if($hour == '12'){
            $hour = 0;
        }
    }
    $new_time = $hour;
    return $new_time;
}

Update with data:

id,"datetime","show_name","sub_name","show_start","show_end","show_start_actual","show_end_actual","day_of_week",sunday,monday,tuesday,wednesday,thursday,friday,saturday,id_personalities,active,"date_added"
1,"2011-06-29 11开发者_StackOverflow中文版:33:46","DJ","DJ","06:00 am","10:00 am","06:00:00","10:00:00",NULL,NULL,1,1,1,1,1,1,0,1,NULL
2,"2011-06-29 11:33:46","DJ","DJ","12:00 pm","03:00 pm","12:00:00","03:00:00",NULL,NULL,1,1,1,1,1,NULL,0,1,NULL
3,"2011-06-29 11:33:46","DJ","DJ","09:00 am","12:00 pm","09:00:00","12:00:00",NULL,NULL,0,0,0,0,0,NULL,NULL,1,NULL
4,"2011-06-29 11:35:17","DJ","DJ","03:00 pm","06:00 pm","15:00:00","18:00:00",NULL,NULL,1,1,1,1,1,0,NULL,1,NULL
5,"2011-06-29 11:35:39","Scott Stevens","Scott Stevens","06:00 pm","09:00 pm","18:00:00","21:00:00",NULL,NULL,0,0,0,0,0,NULL,NULL,1,"2011-06-21 6:15:57"
6,"2011-06-29 11:37:03","DJ","DJ","09:00 pm","12:00 am","21:00:00","00:00:00",NULL,NULL,0,0,0,0,0,NULL,NULL,1,"2011-06-21 6:18:10"
7,"2011-06-29 11:37:17","DJ","DJ","12:00 am","06:00 am","00:00:00","06:00:00",NULL,NULL,1,1,1,1,1,NULL,NULL,NULL,NULL

This is the CSV dump from the table.


Firstly, you can start by simplifying your database design. Below is a suggested alternative.

CREATE TABLE `show` (
  `id` INT UNSIGNED NOT NULL AUTO_INCREMENT,
  `show_name` VARCHAR(64) NOT NULL,
  `sub_name` VARCHAR(64) NOT NULL,
  `show_start` DATETIME NOT NULL,
  `show_end` DATETIME NOT NULL,

/*    OTHER COLUMNS     */

  `datecreated` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
  `dateupdated` TIMESTAMP,
  PRIMARY KEY  (`id`)
) ENGINE=INNODB DEFAULT CHARSET=utf8;

Unless the show is repeated many times in a week, you don't need the week's seven columns. Just get it from show_start itself using a MySQL function.

Once you have done it, getting all the current shows is trivial using either MySQL or PHP. For example, to get all of today's shows using MySQL, the query would be

SELECT * FROM shows
WHERE DATE(NOW()) = DATE(`show_start`)

There are lot of alternatives for DATE in the above query as well.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜