0

I trying to create Show Month in ajax, but the output is a number of month like 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12. I want month be January, February, March .. December

Here's my code:

function list_bulan($id_tahun){
$sql   ="SELECT DISTINCT(MONTH(waktu)) AS bulan FROM aktiv WHERE YEAR(waktu) = $id_tahun ORDER BY MONTH(waktu) ASC";
    $query = $this->db->query($sql);
    $html  = "<option value=''>-- Pilih bulan --</option>";
    foreach ($query->result_array() as $value) {
    if ($value['bulan'] == 1) {
    // january
    echo january;
  }
  $html .=
  "<option value='".$value['bulan']."'>"
  .$value['bulan']. // how to create if in this line? i'm try but with no success
  "</option>";
    }
    echo $html;
}

How create a name of month inside foreach? Any answer?

Andhika R.K.
  • 426
  • 1
  • 11
  • 30

2 Answers2

2
"<option value='".$value['bulan']."'>".date("F", mktime(null, null, null,$value['bulan']))."</option>";

If you want long name use

 date("F", mktime(null, null, null,$your_month_number));

If you want short (Feb, Mar, Apr,)

date("M", mktime(null, null, null,$your_month_number));

But also your echo january will be treated as a constant. Should be echo "january";

But you dont really need that if/statement, if you just use the date('M') I just illustrated, and echo directly from the $value['bulan'] value

Kylie
  • 11,421
  • 11
  • 47
  • 78
0

Why not try

SELECT DISTINCT MONTH(waktu) AS bulan, MONTHNAME(waktu) as bulan_name FROM aktiv WHERE YEAR(waktu) = $id_tahun ORDER BY MONTH(waktu) ASC

As per MySQL MONTHNAME() from numbers.

Community
  • 1
  • 1
artybug
  • 30
  • 2