⚠ 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 to save dates as int but display in nice format



zurtri
  • profile picture
  • Member

Posted 11 April 2012 - 01:26 AM

I have a db that I store dates as int unix times.

When displaying in the crud I would like to display the dates in a nice format such as[b] date("d M Y",$thedate);[/b]

When saving the date, I want to do a [b]strtotime()[/b] conversion so that it is stored in the db as an int.

Is there an easy way to do this? Is there a way to have the field show a nice drop down calender?


Thank you in advance!

Matt
  • profile picture
  • Member

Posted 17 July 2012 - 20:14 PM

I know this is old but for future reference.

You can change the default input box that grocery will create using a callback:

$crud->callback_field('dateStart',array($this,'_callback_dateStart'));


and then do something like this

    function _callback_dateStart($dateStart)
{
if(empty($dateStart))
{
$d = date("d/m/Y");
}
else
{
list($year,$month,$day) = explode('-',$dateStart);
$d = $day.'/'.$month.'/'.$year;
}

return '<input id="field-dateStart" name="dateStart" value="'.$d.'" maxlength="10" class="datepicker-input" type="text">';
}


I also hadd to add a bit of javascript to the page

    $('.datepicker-input').datepicker({
dateFormat: 'dd/mm/yy',
showButtonPanel: true,
changeMonth: true,
changeYear: true
});


for the list view another callback

$crud->callback_column('dateStart',array($this,'_callback_date'));


like this

    function _callback_date($date)
{
if(empty($date))
{
$d = date("d/m/Y");
}
else
{
list($year,$month,$day) = explode('-',$date);
$d = $day.'-'.$month.'-2012';
$daymonth = date("dS F", strtotime($d));
}
return (empty($date)) ? '' : $daymonth . ' ' . $year;
}


In my example I was storing date as varchar and needed to handle dates like 600-04-23 so obviously change it to suit.