简体   繁体   English

比较 codeigniter 和 mySQL 中的日期

[英]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).它有四个时间字段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).我还有 7 个以一周中的日子命名的列(周日、周一等)。 Days of the week are either 1 or 0 to annotate whether the show is on that certain day.星期几为 1 或 0 以注释节目是否在该特定日期。 The actual columns are holding a 24 hour time format, the other times are a varchar field. actual的列采用 24 小时时间格式,其他时间是varchar字段。

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.时间非常基本(即上午 6:00 - 上午 10:00 等)。我在 PHP 和 MySQL 中尝试过这个,我所做的一切都没有工作。 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.这是我目前比较 PHP 中的时间的版本,但我认为如果我能真正将其拉回 mySQL 中会更有效。

Model 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: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.这是表中的 CSV 转储。

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 .只需使用MySQL functionshow_start本身获取它。

Once you have done it, getting all the current shows is trivial using either MySQL or PHP.完成后,使用 MySQL 或 PHP 即可轻松获取所有当前节目。 For example, to get all of today's shows using MySQL, the query would be例如,要使用 MySQL 获取今天的所有节目,查询将是

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

There are lot of alternatives for DATE in the above query as well.在上面的查询中,还有很多 DATE 的替代方案

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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