What about datetime?

#1
[eluser]Unknown[/eluser]
Shouldn't a form_datetime helper be added?

#2
[eluser]Colin Williams[/eluser]
Shouldn't your post, perhaps, outline what this helper would do? ;-P

There are myriad ways to implement date input controls on web forms. What method should a core form_datetime helper implement, and why this specific one? Interested to hear where you're going with this.

#3
[eluser]Unknown[/eluser]
Sorry about that Colin
Actually I'm coming from a Rails background and there a method that formats datetime in a HTML form, As in the following image maybe?
[Image: http://i33.tinypic.com/34ora1j.jpg]
and Why? because It's better to improve the rest of the application instead of implementing such a thing Tongue

ref:
http://api.rubyonrails.com/classes/Actio...elper.html

#4
[eluser]Bramme[/eluser]
Now you're making sense :p

Must say I quite approve of this. However, formatting this back to an actual datetime string (or unix timestamp for those who prefer it) would require an addition to the input library as well...

Unless you'd put a helper in the form helper as well to format the selects into a datetime stamp.

#5
[eluser]nevercraft[/eluser]
I wrote a similar function to do date dropdowns. I've expanded it for the time as well.

Code:
if (! function_exists('form_datetime_dropdown'))
{
    function form_datetime_dropdown($basename = 'datetime_', $selected = array('month' => NULL, 'day' => NULL, 'year' => NULL, 'hour' => NULL, 'min' => NULL, 'sec' => NULL))
    {
        if (is_null($selected['month']))    $selected['month']  = date('m');
        if (is_null($selected['year']))     $selected['year']   = date('Y');
        if (is_null($selected['day']))      $selected['day']    = date('d');
        if (is_null($selected['hour']))     $selected['hour']   = date('H');
        if (is_null($selected['min']))      $selected['min']    = date('i');
        if (is_null($selected['sec']))      $selected['sec']    = date('s');


        $months = array('01' => 'Jan',
                        '02' => 'Feb',
                        '03' => 'Mar',
                        '04' => 'Apr',
                        '05' => 'May',
                        '06' => 'Jun',
                        '07' => 'Jul',
                        '08' => 'Aug',
                        '09' => 'Sep',
                        '10' => 'Oct',
                        '11' => 'Nov',
                        '12' => 'Dec');

        $days = array();
        for ($d = 1; $d <= 31; $d++)
        {
            $day = str_pad($d, 2, '0', STR_PAD_LEFT);
            $days[$day] = $day;
        }

        $years = array();
        for ($y = 1900; $y <= date('Y'); $y++)
        {
            $years[$y] = $y;
        }

        $hours = array();
        for ($h = 0; $h <= 23; $h++)
        {
            $hour = str_pad($h, 2, '0', STR_PAD_LEFT);
            $hours[$hour] = $hour;
        }
        
        $mins = array();
        for ($m = 0; $m <= 59; $m++)
        {  
            $min = str_pad($m, 2, '0', STR_PAD_LEFT);
            $mins[$min] = $min;
        }

        $secs = $mins;

        // Month
        $out = form_dropdown($basename.'[month]', $months, $selected['month']) .' ';

        // Day
        $out .= form_dropdown($basename.'[day]', $days, $selected['day']) .' ';

        // Year
        $out .= form_dropdown($basename.'[year]', $years, $selected['year']).' -- ';

        // Hour
        $out .= form_dropdown($basename.'[hour]', $hours, $selected['hour']).' : ';

        // Minite
        $out .= form_dropdown($basename.'[min]', $mins, $selected['min']).' : ';

        // Second
        $out .= form_dropdown($basename.'[sec]', $secs, $selected['sec']);

        return $out;
    }
}

echo form_datetime_dropdown('start');

And to get a datetime string for use with mysql:
Code:
$start_raw = $this->input->post('start');

$start_time = "{$start_raw['year']}-{$start_raw['month']}-{$start_raw['day']} {$start_raw['hour']}:{$start_raw['min']}:{$start_raw['sec']}";

$start_time_unix = strtotime($start_date);

I'd imagine there are a lot of different ways to do this though, depending on how your db is setup and how you want the forms to look. You might want to use AM/PM time, or add timezones, etc. It might be tough to cover every scenario in the core.


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


Users browsing this thread:
1 Guest(s)


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2019 MyBB Group.