简体   繁体   中英

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. 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.

The value in the database is 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; http://php.net/manual/en/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

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. If you inverted your order it would be set to 1 .

You also should look into using prepared statements for your SQL queries. http://php.net/manual/en/security.database.sql-injection.php

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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