简体   繁体   English

如何将月份名称转换为月份编号

[英]How to convert month name to month number

i want to convert month name to month number. 我想将月份名称转换为月份编号。 By using this code, it is only show a result for december, the other month didnt work. 通过使用此代码,它仅显示12月的结果,其他月份则无效。 But it is work if i change the year. 但是如果我改变一年,那是工作。 For example, i choose November and 2015, the result is December and 2015. and if i choose November and 2014, the result is December and 2014. 例如,我选择11月和2015年,结果是12月和2015年。如果我选​​择11月和2014年,结果是12月和2014年。

The value in the database is 2015-09-28. 数据库中的值为2015-09-28。 i think there is a mistake on how i convert month name to month number. 我认为如何将月份名称转换为月份编号存在错误。 Can someone help me to fix my code. 有人可以帮我修复我的代码。

This is my code : 这是我的代码:

VIEW 视图

  <?php echo form_open("announcement/announcement_result");?>
  <?php echo form_dropdown('m', $m, set_value('m'), 'id="m"'); ?>
  <?php echo form_dropdown('q', $q, set_value('q'), 'id="q"'); ?>
  <?php echo form_submit('search', 'SEARCH', 'class="button expand"'); ?>
  <?php echo form_close(); ?>

CONTROLLER 控制器

function announcement_list()
{

  $data['q'] = array(
    '' => ' Select Year',);   

  for ($i = 0; $i < 10; $i++)
    {
    $date = date('Y') - $i;
    $data['q'][$date] = $date; 
    }

  $m = '';
      $data['m'] = $m;
  $data['m'] = array(
          '' => 'Select Month',
      );
      for ($m = 1; $m <= 12; $m++) {
          $month = date("F", mktime(0, 0, 0, $m));
          $data['m'][$month] = $month;      
    }


    if ($m='December') 
    {
      $m='12';
    }
    else if($m='November') 
    {
      $m='11';
    }
    else if ($m='October') 
    {
      $m='10';
    }
    else if ($m='September') 
    {
      $m='9';
    }
    else if ($m='August') 
    {
      $m='8';
    }
    else if ($m='July') 
    {
      $m='7';
    }
    else if ($m='June') 
    {
      $m='6';
    }
    else if ($m='May') 
    {
      $m='5';
    }
    else if ($m='April') 
    {
      $m='4';
    }
    else if ($m='March') 
    {
      $m='3';
    }                                                           
    else if ($m='February') 
    {
      $m='2';
    }
    else if ($m='January')
    {
      $m='1';
    }
    $data['results'] = $this->news_model->get_announcement_list($config['per_page'], $page);
    }

MODEL 模型

    function get_results($m, $q, $limit=6, $offset=0)
{
    $sql = "SELECT *
        FROM ArkibBerita
        WHERE code='PENGUMUMAN' AND Enable = 'Y' AND Lang ='EN' AND YEAR(BeritaDate)='{$q}' AND MONTH(BeritaDate)='{$m}'
        ORDER BY position ASC
        OFFSET {$offset} ROWS
        FETCH NEXT {$limit} ROWS ONlY";

        $query = $this->db->query($sql);
        return $query->result();

}

What about: 关于什么:

echo date('m', strtotime('january'));

Output: 输出:

01

If you don't want the leading zero use n or see the manual for other usages; 如果您不希望前导零使用n或参阅手册以了解其他用法。 http://php.net/manual/en/function.date.php . http://php.net/manual/zh/function.date.php

In your code you aren't comparing the date, you are setting it. 在代码中,您没有在比较日期,而是在设置日期。

if ($m='December') 

Should be 应该

if ($m=='December') 

One equals sets. 一个等于一组。 Two equals compares. 两个等于比较。 Three equals compares and requires the same variable type. 三个等于比较并且需要相同的变量类型。 http://php.net/manual/en/language.operators.comparison.php http://php.net/manual/zh/language.operators.comparison.php

So on every iteration your $m is going to be 12 because the $m always sets to the string and that is the first condition it hits. 因此,在每次迭代中,您的$m将为12因为$m始终设置为字符串,这是它遇到的第一个条件。 If you inverted your order it would be set to 1 . 如果您取消订单,则将其设置为1

You also should look into using prepared statements for your SQL queries. 您还应该考虑对SQL查询使用准备好的语句。 http://php.net/manual/en/security.database.sql-injection.php http://php.net/manual/zh/security.database.sql-injection.php

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

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