⚠ In case you've missed it, we have migrated to our new website, with a brand new forum. For more details about the migration you can read our blog post for website migration. This is an archived forum. ⚠

  •     

profile picture

How can I convert datetime field to "Y-m-d" formate



Sonelal Singh
  • profile picture
  • Member

Posted 01 July 2015 - 11:01 AM

Hi,

 

I have a field(create_date) in USER table.This stores default datetime(2015-07-01 03:05:34) value in the table as its value.

When I am displaying this value in front end it display as "01/07/2015 - 00:00". But I want to display date as "2015-07-01", To achieve this I used this code $crud->field_type('created_date', 'date') for formatting.So now it display as "01/07/2015".

 

So my question is how can I display this date as "2015-07-01".

 

 

 


Paul Savostin
  • profile picture
  • Member

Posted 01 July 2015 - 12:07 PM

Hi!

Frontend - you mean on the site part or admin site part?

On the site you can change output do smth date('Y-m-d', strtotime($your_date));

On the admin site in table like column you can add

->callback_columns('your_date', array($this, 'callback_date'));

function callback_date($val, $row)
{
        return date('Y-m-d', strtotime($val);
}

in read state do the same like above only diff it will be for callback_read_fields


in insert or update state nothing to do cause you have the datetime picker.

Hope it helps


Sonelal Singh
  • profile picture
  • Member

Posted 02 July 2015 - 06:16 AM

Hi Paul,

 

Thanks for your reply.

 

In database my field name is "create_date" and date type is "timestamp" and default value is "CURRENT_TIMESTAMP".

 

But when I fetch and displaying this field in front end it display like "30/11/1999" (d/m/y) format.

 

I want to display this field like "1999-11-30".

 

 

 

I have applied you suggestion in my code like below -

 

$crud->callback_columns('created_date', array($this, 'callback_date'));

 

and in same controller I have created callback function like below 

 
public function callback_date($val, $row)
{
        return date('Y-m-d', strtotime($val));
}

 

But I am getting below error

Call to undefined method Grocery_CRUD::callback_columns() 


Paul Savostin
  • profile picture
  • Member

Posted 02 July 2015 - 08:11 AM

Hi! If in database you have timestamp then you dont need to use function "strtotime" - it will be just date('Y-m-d', $your_timestamp_date);

I have misprint in word callback_column - it will be without "S" at the end (You can see list function on the GC site)



Man, learn PHP and GC, I see that you just copy paste and dont thinking at all...


Sonelal Singh
  • profile picture
  • Member

Posted 02 July 2015 - 08:50 AM

Hey Paul,

 

Thanks for your help.

It worked   :) 


Paul Savostin
  • profile picture
  • Member

Posted 02 July 2015 - 13:58 PM

you're welcome!


Margarita Mota
  • profile picture
  • Member

Posted 24 January 2017 - 17:19 PM

Thanks, that work for the list, but if I go inside the record ( view read) the date keep the unix format, somebody can help me? I want to see the date in normal format when I go into the view of a simple record