【问题标题】:How to compare date from current year with column from database如何将当年的日期与数据库中的列进行比较
【发布时间】:2015-07-11 15:16:55
【问题描述】:

我正在尝试根据这种情况从数据库中获取数据:

 if($now < "'.$current_year . '-09-15")

我必须显示最后一次学习的数据。 我想检查当前日期是否小于 15.September 当年以及是否要显示数据库中的数据以进行学习。 但它没有显示正确的结果。 我的代码是:

<?php
 $date = new DateTime("now"); 
 $now=$date->format('Y-m-d');
 $current_year = $date->format('Y');
 $last_year =  $current_year -1;   


 if($now < "'.$current_year . '-09-15") {

      $this->db->where('student_surveys.created_at < "'.$current_year . '-09-15" AND student_surveys.created_at > "'.$last_year . '-09-15"');
 }
 else {
     
      $this->db->where('student_surveys.created_at > "'.$current_year . '-09-15"');
 }

【问题讨论】:

  • 你得到什么结果?
  • 我创建了解决方案:$this->db->where('student_surveys.created_at

标签: php date compare


【解决方案1】:

比较语句中日期的格式有误。当我尝试运行代码时,在 if 部分打印“true”,在 else 部分打印“false”,它返回 false(这是不正确的输出)。 试试这个:

<?php
 $date = new DateTime("now"); 
 $now=$date->format('Y-m-d');
 $current_year = $date->format('Y');
 $last_year =  $current_year -1;   
 $yourdate = $current_year."-09-15";
 if($now < $yourdate) {         
      $this->db->where('student_surveys.created_at < "'.$current_year . '-09-15" AND student_surveys.created_at > "'.$last_year . '-09-15"');
 }
 else {     
      $this->db->where('student_surveys.created_at > "'.$current_year . '-09-15"');
 }
?>

【讨论】:

  • 谢谢,它正在工作!我对引号有疑问。 :)
猜你喜欢
  • 2019-06-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多