1

CLICK TO SEE THE OUTPUT

As you can see my date.of.birth format is Y-m-d, so I hope to change my D.O.B (tarikh lahir) format to d-m-Y,please help.

This is model : Search_model.php

<?php

class Search_model extends CI_Model {

    public function get_results($search_term='default')
    {
        // Use the Active Record class for safer queries.
        // $this->load->helper('share_function');
        $this->db->select('pesakit.rn,pesakit.nama,pesakit.tarikhlahir,jantina.nama as jantina,agama.nama as agama,bangsa.nama as bangsa');
        $this->db->from('lifeline.pesakit as pesakit');
        $this->db->join('jantina','jantina.kod = pesakit.jantina');
         $this->db->join('agama','agama.kod = pesakit.agama');
          $this->db->join('bangsa','bangsa.kod = pesakit.bangsa');
        $this->db->where('rn',alphaToNumber($search_term));


        // Execute the query.
        $query = $this->db->get();

        // Return the results.
        return $query->result_array();
    }

}



This is view : search_results.php

<div> 

    <?php
        foreach ($results as $val)
        {
            echo 'RN : '; echo numberToAlpha( $val['rn']); echo "<br/>";
            echo 'Name : '; echo  $val['nama']; echo "<br/>";
            echo 'Date.of.Birth : '; echo $val['tarikhlahir']; echo "<br/>";
            echo 'Age:' ; echo calculateCurrentAge ($val['tarikhlahir']); echo "<br/>";
            echo 'Gender : '; echo $val['jantina']; echo "<br/>";
            echo 'Race : '; echo $val['bangsa']; echo "<br/>";
            echo 'Religion : '; echo $val['agama'];
        }
    ?>
</div>
Micho
  • 3,929
  • 13
  • 37
  • 40
May Xyan
  • 21
  • 1
  • 1
  • 2

7 Answers7

1

Check this code. Hopefully, it will help you.

$dob = "2012-03-08";
echo date("d-m-Y", strtotime($dob));
//output date
08-03-2012
Amanullah Aman
  • 633
  • 1
  • 12
  • 29
1

You need to create a function dateFormat in Helper.

function dateFormat($date, $format = 'd-M-Y'){
    return date($format, strtotime($date));
}

We can call this :

<td><?php echo dateFormat($val['tarikhlahir']);?></td>

Using above function, date will be returned as its in default parameter.

While it can be changed where we will call this function.

For example :

<td><?php echo dateFormat($val['tarikhlahir'], 'm-d-Y');?></td>
Naveed Ramzan
  • 3,565
  • 3
  • 25
  • 30
1

Try not to use the semicolon to concatenate the strings.

Use "Dot" to do this.

echo 'Date.of.Birth : ' . date('d m Y', strtotime($val['tarikhlahir']) . "<br/>";
Rodrigo Prazim
  • 788
  • 7
  • 14
0
date('d-m-Y', strtotime($the_date_you_want_to_convert));

http://php.net/manual/en/function.strtotime.php

Alex
  • 9,215
  • 8
  • 39
  • 82
0

As per the @Alex answer.

As per my knowledge, there is no separate date function in CodeIgniter. You can use PHP date function.

http://php.net/manual/en/function.date.php

http://php.net/manual/en/function.strtotime.php

$val['tarikhlahir']='1959-01-30';
$date=date('d-m-Y',strtotime($val['tarikhlahir']));
echo $date;
Naren Verma
  • 2,205
  • 5
  • 38
  • 95
0

Please replace your the code as given below

<div> 

        <?php
            foreach ($results as $val)
            {
                echo 'RN : '; echo numberToAlpha( $val['rn']); echo "<br/>";
                echo 'Name : '; echo  $val['nama']; echo "<br/>";
                $originalDate = $val['tarikhlahir'];
                $newDate = date("d-m-Y", strtotime($originalDate));
                echo 'Date.of.Birth : '; echo $newDate; echo "<br/>";
                echo 'Age:' ; echo calculateCurrentAge ($val['tarikhlahir']); echo "<br/>";
                echo 'Gender : '; echo $val['jantina']; echo "<br/>";
                echo 'Race : '; echo $val['bangsa']; echo "<br/>";
                echo 'Religion : '; echo $val['agama'];
            }
        ?>
    </div>

This may help you..Thanks!

Abdul Salam
  • 406
  • 2
  • 15
0

since nobody mentioned it - imho the safest way to do this is the DateTime::createFromFormat function.

Something like that should work

<div> 

    <?php
        foreach ($results as $val)
        {
            $objDateTime = DateTime::createFromFormat('Y-m-d', $val['tarikhlahir']);


            echo 'RN : '; echo numberToAlpha( $val['rn']); echo "<br/>";
            echo 'Name : '; echo  $val['nama']; echo "<br/>";
            echo 'Date.of.Birth : '; echo $objDateTime->format('d-m-Y'); echo "<br/>";
            echo 'Age:' ; echo calculateCurrentAge ($val['tarikhlahir']); echo "<br/>";
            echo 'Gender : '; echo $val['jantina']; echo "<br/>";
            echo 'Race : '; echo $val['bangsa']; echo "<br/>";
            echo 'Religion : '; echo $val['agama'];
        }
    ?>
</div>
Atural
  • 5,389
  • 5
  • 18
  • 35