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



  • 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



  • 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:


and then do something like this

function _callback_dateStart($dateStart)
            $d = date("d/m/Y");
            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

            dateFormat: 'dd/mm/yy',
            showButtonPanel: true,
            changeMonth: true,
            changeYear: true

for the list view another callback


like this

function _callback_date($date)
            $d = date("d/m/Y");
            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