1

I'am trying to print my timestamp formated date & time from mysql database in php Codeigniter but this will give me a error message :

A non well formed numeric value encountered

My code:

if ($userTasks) {
   foreach ($userTasks as $key) {
       echo time_convert($key->t_time);
   }
}


public function time_convert($timestamp){
   return date('l Y/m/d H:i', $timestamp);
}
Boken
  • 4,825
  • 10
  • 32
  • 42

2 Answers2

0

You can use either of two ways. First is DATE FORMAT function in your SQL, like this.

$this->db->select(' DATE_FORMAT(orderdate, '%Y-%m-%d') as orderDate')

To learn more about it Read here

Another way to it is through PHP you can use PHP's DateTime class like this

    public function time_convert($timestamp){
       // Create datetime object
       phpDatetime = DateTime::createFromFormat ("Y-m-d H:i:s", $timestamp);

      // return datetime in your  required format
      return phpDatetime->format('l Y/m/d H:i');
}
mail2bapi
  • 1,547
  • 1
  • 12
  • 18
0

There are codeigniter DateTime library and helper as well but I prefer using normal PHP for this basic functionalities. Basic two ways:

  1. Convert your mysql timestamp to Unix Timestamp using "strtotime()" and use "date()" function to convert unix timestamp to formatted date.

    $unix_ts=strtotime('2020-07-01 20:46:23');  
    echo date("Y-m-d",$unix_ts);
    
  2. Create date object from mysql timestamp using "create_date()" and use "date_format()" function which takes date object as its parameter.

    $date = date_create('2020-07-01 20:46:23');
    echo date_format($date, 'Y-m-d');
    
prab
  • 290
  • 1
  • 12