Jump to content


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

date formatting

  • Please log in to reply
6 replies to this topic

#1 Sonelal Singh

Sonelal Singh

    Newbie

  • Members
  • Pip
  • 8 posts

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

 

 

 



#2 Paul Savostin

Paul Savostin

    Advanced Member

  • Members
  • PipPipPip
  • 229 posts
  • LocationUkraine

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



#3 Sonelal Singh

Sonelal Singh

    Newbie

  • Members
  • Pip
  • 8 posts

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() 



#4 Paul Savostin

Paul Savostin

    Advanced Member

  • Members
  • PipPipPip
  • 229 posts
  • LocationUkraine

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



#5 Sonelal Singh

Sonelal Singh

    Newbie

  • Members
  • Pip
  • 8 posts

Posted 02 July 2015 - 08:50 AM

Hey Paul,

 

Thanks for your help.

It worked   :) 



#6 Paul Savostin

Paul Savostin

    Advanced Member

  • Members
  • PipPipPip
  • 229 posts
  • LocationUkraine

Posted 02 July 2015 - 01:58 PM

you're welcome!



#7 Margarita Mota

Margarita Mota

    Newbie

  • Members
  • Pip
  • 2 posts

Posted 24 January 2017 - 05: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






0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users