blairmeister
blairmeister

Reputation: 915

CakePHP Date Array

        $dts = new DateTime(AppController::getSetting('event_start'));
        $dtf = new DateTime(AppController::getSetting('event_finish'));        

        //CONTROLLER
...
        $weekdays = array(0,1,2,3,4,5,6);

        $dates = array();
        $today = strtotime(date("Y-m-d", $dts->getTimestamp()));
        $end_date = strtotime(date("Y-m-d", $dtf->getTimestamp()));
        while($today <= $end_date) 
        {
            $weekday = date("w", $today);
            if (in_array($weekday, $weekdays)) 
            {
                array_push($dates, date("Y-m-d", $today));
            }
            $today += 86400;
        }

        $this->set('dates', $dates);
...



//VIEW
...
    echo $this->Form->input('date', array('options'=> $dates));
...

dts and dtf are a start and finish date I get from my database...

When I select a date from my drop box in my view, it submits ok but all i get in my database is 0000-00-00?

What am I doing wrong here?

EDIT

My array out puts this with

Debugger::dump($dates);

    array(
(int) 0 => '2013-04-01',
(int) 1 => '2013-04-02',
(int) 2 => '2013-04-03',
(int) 3 => '2013-04-04',
(int) 4 => '2013-04-05',
(int) 5 => '2013-04-06',
(int) 6 => '2013-04-07'
)

EDIT This is what my query looks like

INSERT INTO cake.tickets (first_name, last_name, email, phone, date, quantity) VALUES ('brbt', 'trbb', '[email protected]', 765657, //THIS IS THE DATE//2, 2).

It seems to only input the key?

Upvotes: 1

Views: 599

Answers (1)

jeremyharris
jeremyharris

Reputation: 7882

Make your keys and your values the same. The key is what is saved, the value is what is seen by the user.

$dates = array();
$today = strtotime(date("Y-m-d", $dts->getTimestamp()));
$end_date = strtotime(date("Y-m-d", $dtf->getTimestamp()));
while($today <= $end_date) 
{
    $weekday = date("w", $today);
    if (in_array($weekday, $weekdays)) 
    {
        $dates[date("Y-m-d", $today)] = date("Y-m-d", $today);
    }
    $today += 86400;
}
$this->set('dates', $dates);

Your date array should now look something like this:

array(
 '2013-04-01' => '2013-04-01',
 '2013-04-02' => '2013-04-02',
 '2013-04-03' => '2013-04-03',
 '2013-04-04' => '2013-04-04',
 '2013-04-05' => '2013-04-05',
 '2013-04-06' => '2013-04-06',
 '2013-04-07'=> '2013-04-07'
)

Which will generate the proper select options.

Upvotes: 2

Related Questions