如果日期跨越多年,则日期之间不起作用

时间:2013-12-10 12:48:34

标签: php mysql

好吧我有一个脚本(这是分手的)

$monday = date('m/d/y',strtotime('this week monday'));
$this_monday = date('y-m-d',strtotime('this week monday'));
$tuesday = date('m/d/y',strtotime('this week tuesday'));
$this_tuesday = date('y-m-d',strtotime('this week tuesday'));
$wednesday = date('m/d/y',strtotime('this week wednesday'));
$this_wednesday = date('y-m-d',strtotime('this week wednesday'));
$thursday = date('m/d/y',strtotime('this week thursday'));
$this_thursday = date('y-m-d',strtotime('this week thursday'));
$friday = date('m/d/y',strtotime('this week friday'));
$this_friday = date('y-m-d',strtotime('this week friday'));
$saturday = date('m/d/y',strtotime('this week saturday'));
$this_saturday = date('y-m-d',strtotime('this week saturday'));
$sunday = date('m/d/y',strtotime('this week sunday'));
$this_sunday = date('y-m-d',strtotime('this week sunday'));

$fourth_monday = date('m/d/y',strtotime($this_monday.'+21 days'));
$fourth_tuesday = date('m/d/y',strtotime($this_tuesday.'+21 days'));
$fourth_wednesday = date('m/d/y',strtotime($this_wednesday.'+21 days'));
$fourth_thursday = date('m/d/y',strtotime($this_thursday.'+21 days'));
$fourth_friday = date('m/d/y',strtotime($this_friday.'+21 days'));
$fourth_saturday = date('m/d/y',strtotime($this_saturday.'+21 days'));
$fourth_sunday = date('m/d/y',strtotime($this_sunday.'+21 days'));


$fourthSchedule = DB::getInstance()->getAssoc("SELECT * FROM sched WHERE full_name = ? AND date between ? and ?", array(
        $tech, $fourth_monday, $fourth_sunday));
    foreach($fourthSchedule->results() as $results) {
        $sched_rows_4[$tech][] = $results;
    }

如果它保留在当前年份,那么效果很好。问题是,如果那一周(就像今年那样),跨越12月底和1月初之间,它将不会返回结果。

如何解决这个问题,我们将非常感激。

2 个答案:

答案 0 :(得分:0)

这可能有帮助

<?php

//this is current day
$from_here_to = strtotime(date('Y-m-d'));

$monday = date('m/d/y',strtotime('this week monday', $from_here_to));

echo $monday.'<br/>';

//change your year 
$from_here_to = strtotime('2012-12-10');

$monday = date('m/d/y',strtotime('this week monday', $from_here_to));

echo $monday;

答案 1 :(得分:0)

从你的回答到我的评论,答案很简单:

按字母顺序排列,01-01-2014出现在2013年12月31日之前,因此您的比较结果(between<>)将始终非常不稳定。从一年到另一年,这种情况看起来更明显,但也可能在同一年内发生,具体取决于您当前的日期格式...(比利时的标准是dd-mm-yyyy,所以15-12-2013会来在2013年1月21日之前,例如...)

编辑(从备注中添加详细信息):

最佳解决方案是使用日期类型字段。如果您真的希望使用varchar,请使用符合字母顺序的日期格式:yyyy-mm-dd

在这两种情况下,您都需要运行查询来更新当前现有的数据。要转换为DateTime格式,请创建一个DataTime字段,并通过从当前varchar字段中转换值来更新它。如果保留varchar类型,则只需使用字符串操作更新字段,以根据需要对日期值进行排序。

在操作任何东西之前不要忘记做备份: - )