dongtiandexue123456 2013-10-09 14:09
浏览 36

日期范围字段无法正常工作

I'm a student web programmer and am having an issue with a task I was recently assigned.The previous person who held my position had created a news reporting site that is a little buggy.

The issue with the site is that when you specify a date range for which news articles to display, the dates will either revert to what they were previously or they will change to something random. Trying to set a date before 2010 is the only way to get this issue to occur every time. Sometimes it will work for many uses before having an issue.

Just to clarify, I'm not looking/hoping for someone to give me an outright answer/do my work for me, especially since I really am trying to learn and improve my skills. I would just really appreciate if someone could point me in the right direction to solving this problem.

Here are all the references to the date from the file I'm looking over:

// Create dates
$year = ( isset( $get['year'] ) ) ? $get['year'] : date("Y");
$year2 = ( isset( $get['year2'] ) ) ? $get['year2'] : date("Y");
$month = ( isset( $get['month'] ) ) ? $get['month'] : date("m");
$month2 = ( isset( $get['month2'] ) ) ? $get['month2'] : date("m");
$day = ( isset( $get['day'] ) ) ? $get['day'] : date("d");
$day2 = ( isset( $get['day2'] ) ) ? $get['day2'] : date("d");

//create first and second dates for range
$t_current = mktime(0, 0, 0, $month, $day, $year); // Current month Unix timestamp
$t_next = mktime(0, 0, 0, $month2, $day2+1, $year2); // date2 unix timestamp for date range

// Criteria
if ( $author_url) {
    $this->EE->db -> where ( $author_name_field_id, $author_name);

$this->EE->db
    -> where ("exp_channel_titles.entry_date >= $t_current")
    -> where ("exp_channel_titles.entry_date <= $t_next");

}

else {
    $this->EE->db
    -> where ("exp_channel_titles.entry_date >= $t_current")
    -> where ("exp_channel_titles.entry_date <= $t_next");
}

Template:

<div class="datelists">
    <select id="month" name="month" style="display:none">
<?php
//lists months
            for ($i = 0; $i <= 11; ++$i) 
            {
                $time = strtotime(sprintf('+%d months', $i));
                $value = date('m', $time);
                $label = date('F', $time);

//if month is set stay on that month    
                if($month==$value)
                    { printf('<option value="%s" selected="selected">%s</option>' , $value, $label);
                    }
                else
                    {printf('<option value="%s">%s</option>', $value, $label);}
            }

  ?>
//first month selected instead of blank
        $("#target option:first")

    </select>
    <select id="day" name="day" style="display:none">   
<?php
//lists days
            for ($i = 0; $i <= 31; ++$i) 
            {

                $time = strtotime(sprintf('+%d days', $i));
                $value = date('d', $time);
                $label = date('d', $time);
//if day is set stay on that day            
                if($day==$value)
                    { printf('<option value="%s" selected="selected">%s</option>' , $value, $label);
                    }
                else
                    {printf('<option value="%s">%s</option>', $value, $label);} 
            }           
//first year selected instead of blank
  ?>
    $("#target option:first")
    </select>
    <select id="year" name="year" style="display:none"> 
<?php   
//lists years
            for ($i = 0; $i <= 3; ++$i) 
            {                 
                $time = strtotime(sprintf('-%d years', $i));
                $value = date('Y', $time);
                $label = date('Y', $time);  
//if year is set stay on that year          
                if($year==$value)
                    { printf('<option value="%s" selected="selected">%s</option>' , $value, $label);
                    }
                else
                    {printf('<option value="%s">%s</option>', $value, $label);}         
            }

//first year selected instead of blank
  ?>
    $("#target option:first")
    </select>
    <input type="hidden" id="datepicker" />
    <select id="month2" name="month2" style="display:none">
<?php
//lists months
            for ($i = 0; $i <= 11; ++$i) 
            {
                $time = strtotime(sprintf('+%d months', $i));
                $value = date('m', $time);
                $label = date('F', $time);
//if month is set stay on that month        
                if($month2==$value)
                    { printf('<option value="%s" selected="selected">%s</option>' , $value, $label);
                    }
                else
                    {printf('<option value="%s">%s</option>', $value, $label);}
            }
  ?>
//first month selected instead of blank
        $("#target option:first")
    </select>
    <select id="day2" name="day2" style="display:none">
<?php
//lists days
            for ($i = 0; $i <= 31; ++$i) 
            {     
                $time = strtotime(sprintf('+%d days', $i));
                $value = date('d', $time);
                $label = date('d', $time);
//if day is set stay on that day            
                if($day2==$value)
                    { printf('<option value="%s" selected="selected">%s</option>' , $value, $label);
                    }
                else
                    {printf('<option value="%s">%s</option>', $value, $label);}     
            }
//first year selected instead of blank
  ?>
    $("#target option:first")
    </select>
    <select id="year2" name="year2" style="display:none">
<?php
//lists years
            for ($i = 0; $i <= 3; ++$i) 
            {             
                $time = strtotime(sprintf('-%d years', $i));
                $value = date('Y', $time);
                $label = date('Y', $time);  
//if year is set stay on that year          
                if($year2==$value)
                    { printf('<option value="%s" selected="selected">%s</option>' , $value, $label);
                    }
                else
                    {printf('<option value="%s">%s</option>', $value, $label);}             
            }               
//first year selected instead of blank
  ?>
    $("#target option:first")
    </select>
    <input type="hidden" id="datepicker" />  
</div>
<label for="from">From</label>
<input type="text" id="from" value="<?php echo $month."/".$day."/".$year; ?>" />
<label for="to">to</label>
<input type="text" id="to" value="<?php echo $month2."/".$day2."/".$year2; ?>" />               
<input type="submit" value="Filter" />
        </form>
               </p>
</div>     
<!--<h1><?php echo "$month/$day/$year - $month2/$day2/$year2" . ' <br/>'?></h1>-->
<?php
    if ( isset($get['institute']) && is_numeric( $get['institute'] )  ) {

        echo "<h2><center>" . $institute_cat . "</center></h2>";
    }

    if ( empty($entries) ) {
        echo "<h2><center>No articles found</center></h2>";
    }
    else {
?>  

Javascript:

<!-- javascript datepicker -->
<link rel="stylesheet" type="text/css" href="http://code.jquery.com/ui/1.10.3/themes/smoothness/jquery-ui.css" /></link>
<script src="http://code.jquery.com/ui/1.10.3/jquery-ui.js"></script>
<link rel="stylesheet" type="text/css" href="http://expeng.anr.msu.edu/css/sitewide/datepicker.css" />  </link>
<script type="text/javascript">
$(document).ready(function(){
    $('#from').datepicker({
        defaultDate: "+1w",
        changeMonth: true,
        numberOfMonths: 1,
        onSelect: function(dateText, inst) {
                //dateText comes in as MM/DD/YY
                var datePieces = dateText.split('/');
                var month = datePieces[0];
                var day = datePieces[1];
                var year = datePieces[2];
                //define select option values for
                $('select#month').val(month);
                $('select#day').val(day);
                $('select#year').val(year);
        },                  
    }); 
    $('#to').datepicker({
        defaultDate: "+1w",
        changeMonth: true,
        numberOfMonths: 1,
        onSelect: function(dateText, inst) {
                //dateText comes in as MM/DD/YY
                var datePieces = dateText.split('/');
                var month2 = datePieces[0];
                var day2 = datePieces[1];
                var year2 = datePieces[2];
                //define select option values for
                $('select#month2').val(month2);
                $('select#day2').val(day2);
                $('select#year2').val(year2);
         },     
    }); 
});
</script>   

http://msue.anr.msu.edu/news/report is the website just in case anyone was interested in seeing the problem for themselves.

Thanks in advance!

  • 写回答

2条回答 默认 最新

  • duancai9010 2013-10-09 14:23
    关注

    at this point in the code:

    //create first and second dates for range
    $t_current = mktime(0, 0, 0, $month, $day, $year); // Current month Unix timestamp
    $t_next = mktime(0, 0, 0, $month2, $day2+1, $year2); // date2 unix timestamp for date range
    

    I would definately add a check to ensure that $t_next is later than $t_current as that would be the first logic problem I would look for.

    if($t_current>$t_next){
        // swap them over
        $tmp=$t_current;
        $t_current=$t_next;
        $t_next=$tmp;
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。