Jump to content


How to save dates as int but display in nice format

date strtotime

  • Please log in to reply
1 reply to this topic

#1 zurtri

zurtri

    Newbie

  • Members
  • Pip
  • 2 posts

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 date("d M Y",$thedate);

When saving the date, I want to do a strtotime() 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!

#2 Matt

Matt

    Member

  • Members
  • PipPip
  • 14 posts

Posted 17 July 2012 - 08: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.





Also tagged with one or more of these keywords: date, strtotime

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users