1
        $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?

3
  • Thats not daylight saving time proof. Use the DateTime class for the +1 day operation. In general, replace all calls to strtotime() and date() by the proper alternatives of DateTime Commented Mar 5, 2013 at 20:18
  • What does your SQL query for the insert look like? Regular CakePHP date-inputs use dropdowns and send dates back as an array (year => xx, month=> xx, day => xx), therefore the Model might not pick up the right date value when sending it as a string Commented Mar 5, 2013 at 20:31
  • 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? Commented Mar 5, 2013 at 20:35

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Haha yeah we all miss the little stuff from time to time. The more eyes the better.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.