Welcome Guest, Not a member yet? Register   Sign In
What about datetime?
#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.


Messages In This Thread
What about datetime? - by El Forum - 08-30-2008, 12:15 AM
What about datetime? - by El Forum - 08-30-2008, 03:14 AM
What about datetime? - by El Forum - 08-30-2008, 05:46 AM
What about datetime? - by El Forum - 08-30-2008, 11:23 AM
What about datetime? - by El Forum - 09-06-2008, 10:10 PM



Theme © iAndrew 2016 - Forum software by © MyBB