⚠ 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

read state doesn't use the configured date format



Carlos Castell
  • profile picture
  • Member

Posted 07 April 2015 - 21:32 PM

Hi,

 

I have configured a date format in the grocery_crud.php config file:

$config['grocery_crud_default_date_format']  = 'd M Y';

This is working fine for the "list" state (table view), however it is not the case for the "read" state, it keeps looking as "yyy-mm-dd". 

 

 

Am I missing something?

 

 

Btw, I am using datatables.


Carlos Castell
  • profile picture
  • Member

Posted 08 April 2015 - 21:47 PM

I have assumed that the read state will never format the date so I decided to do it myself by modifying the read.php file under assets\grocery_crud\themes\datatables\views.

 

You just need to add the below code in the div that prints the values out, around line 34.

 

Before the change:

<div class='form-input-box' id="<?php echo $field->field_name; ?>_input_box">
<?php echo $input_fields[$field->field_name]->input?>

After the change:

				<div class='form-input-box' id="<?php echo $field->field_name; ?>_input_box">
					<?php 
						$res = preg_match('/[0-9]{4}\-[0-9]{2}\-[0-9]{2}/',$input_fields[$field->field_name]->input,$var);
						if($res==1) {
							if(!preg_match('/0000/',$var[0])) {
								$ci = &get_instance();
								$dt = new DateTime($var[0]);
								$output = $dt->format($ci->config->item('grocery_crud_default_date_format'));
								$res = preg_match('/[0-9]{4}\-[0-9]{2}\-[0-9]{2}\s[0-2][0-9]\:[0-6][0-9]/',$input_fields[$field->field_name]->input,$var);
								if($res==1) {
									$dt = new DateTime($var[0]);
									$output = $dt->format($ci->config->item('grocery_crud_default_datetime_format'));
								}
								echo $output;
							} else 
								echo "";
						} else
							echo $input_fields[$field->field_name]->input?>

Now you can test the date format in the read state... and make sure you have defined the format you want in the application/config/grocery_crud.php file, I used:

 

$config['grocery_crud_default_date_format']  = 'd M Y';

$config['grocery_crud_default_datetime_format'] = 'd M Y - H:i';

 

Cheers!