event schedule php MySQL keeping track of upcoming events -
i have been working on event schedule php , mysql goal able have background of website change each event such halloween christmas , on have come 1 work month needing workout day ignoring year
<?php $con = mysql_connect(mysql_host,mysql_username,mysql_password); mysql_select_db(mysql_database, $con); $result = mysql_query('select * event_schedule month(start) <= month(now()) , month(end) >= month(now())') or die('query failed: ' . mysql_error()); $edit_theme_row = mysql_fetch_array($result) ?>
i have tried adding day in code
$result = mysql_query('select * event_schedule (month(start) <= month(now()) , day(start) <= day(now())) , (month(end) >= month(now()) , day(end) >= day(now()))') or die('query failed: ' . mysql_error()); $edit_theme_row = mysql_fetch_array($result) ?>
but seem ignore event using template date in mysql example 2015-10-28 2015-11-02 halloween 2015-12-01 2015-12-26 christmas ignoring year each year change on month , day
i hope understand problem correctly. first thing have seen use functions in where on database fields. not idea. mysql must read every record (full table scan) , cant use index this.
the second thing not normalize start , end date of each event in 2 separate fields easy compare. can store dates application in second fields normalized year ie. '1970' can easy compare or use persistent fields in mysql mysql can you.
here sample
create table `table1` ( `nr` int(11) unsigned not null auto_increment, `event_name` varchar(32) default null, `event_start` date not null default '0000-00-00', `event_end` date not null default '0000-00-00', `norm_start` date ( date_format(event_start,'1970-%m-%d') ) persistent, `norm_end` date ( date_format(event_end,'1970-%m-%d') ) persistent, primary key (`nr`), key `event_start` (`event_start`,`event_end`), key `norm_start` (`norm_start`,`norm_end`) ) engine=innodb default charset=utf8;
now insert row
insert `table1` ( `event_name`, `event_start`, `event_end`) values ('hallo', '2015-10-31', '2015-10-31');
the reseult
mariadb > select * table1; +----+------------+-------------+------------+------------+------------+ | nr | event_name | event_start | event_end | norm_start | norm_end | +----+------------+-------------+------------+------------+------------+ | 4 | hallo | 2015-10-31 | 2015-10-31 | 1970-10-31 | 1970-10-31 | +----+------------+-------------+------------+------------+------------+ 1 row in set (0.00 sec)
now can directly compare dates
select * table1 date_format(now(),'1970-%m-%d') between norm_start , norm_end;
so can events. thing when event overlaps year ( 2015-12-30 - 2016-01-07 ) mus put 2 rows in eventtable.
please let me if wars want
Comments
Post a Comment