Inserting data into database

I’m trying to build a calendar of sorts where people can add in their own events. Most of it works except for the date field always uses today’s date.

I’ve got this:

public function saveAction(Request $request, Application $app)
  {
    $event = $request->get('form')['event'];

    if ($event) {
      $dd = \\Cb2b\\LatestEventsQuery::create()
        ->filterByEvent($event)
        ->findOneOrCreate();
      $dd->setEventDate(date('Y-m-d'));
      $dd->setEvent($event);
      $dd->save();
    }

    return $app->redirect('/latest_events');
  }

but it uses today’s date. I’ve tried changing it to this:

public function saveAction(Request $request, Application $app)
  {
    $event = $request->get('form')['event'];

    if ($event) {
      $dd = \\Cb2b\\LatestEventsQuery::create()
        ->filterByEvent($event)
        ->findOneOrCreate();
      $dd->setEventDate(get('form')['event_date']);
      $dd->setEvent($event);
      $dd->save();
    }

    return $app->redirect('/latest_events');
  }

which gives this error:

[14-Jul-2014 15:29:00 Europe/London] PHP Fatal error: Call to undefined function Cb2b\LatestEvents\Controller\get() in /home/ubuntu/web/src/LatestEvents/Controller/LatestEventsController.php on line 57

line 57 is:

$dd->setEventDate(get('form')['event_date']);

and this:

public function saveAction(Request $request, Application $app)
  {
    $event = $request->get('form')['event']
    $event_date = $request->get('form')['event_date'];

    if ($event) {
      $dd = \\Cb2b\\LatestEventsQuery::create()
        ->filterByEvent($event)
 //       ->filterByDate(array('date'=>event_date('Y-m-d 00:00'), 'date'=>event_date('Y-m-d 23:59')))
        ->findOneOrCreate();
      $dd->setEventDate(array($event_date);
      $dd->setEvent($event);
      $dd->save();
    }

    return $app->redirect('/latest_events');
  }

but this gives me the error:

[14-Jul-2014 15:01:04 Europe/London] PHP Parse error: syntax error, unexpected ‘$event_date’ (T_VARIABLE) in /home/ubuntu/web/src/LatestEvents/Controller/LatestEventsController.php on line 52

Line 52 is:

$event_date = $request->get('form')['event_date'];

I’ve tried so many different ways of but no matter what I do it either throws up an error or inserts today’s date.

The form where people add the events looks like this:

<form method="post" id="events">
  <div class="form-group"><label for="form_event_date" class="required control-label">Event date</label><input type="text" id="form_event_date" name="form[event_date]" required="required" class="form-control"></div><div class="form-group"><label for="form_event" class="required control-label">Event</label><textarea id="form_event" name="form[event]" required="required" class="form-control"></textarea></div><input type="hidden" id="form__token" name="form[_token]" class="form-control" value="292270f1fedc80a05e8f4607a90bf00dd4857a30">
  <div class="form-group">
    <button type="submit" class="btn btn-success">Save</button>
  </div>
</form>

I’d be extremely grateful for any help with this please.

Well date(‘Y-m-d’) will give today’s date. So it’s not surprising $dd->setEventDate(date(‘Y-m-d’)) puts in today’s date. To get date() to output some other date you need to give it, as the second argument, the date you want – in Unix time stamp format. date(‘Y-m-d’, $timestamp). I’m less sure on the get() bit but this might work:


$date = $request->get('form')['event_date']; 
$ts = strtotime($date);
$dd->setEventDate(date('Y-m-d',$ts));

That relies on the user’s date text being supplied in a format that strtotime() recognises. It might be a good idea to change the form to pop ups for the month and date possibly.

Thank you that works great, I have to write the date as mm/dd/yyyy but I want to be able to write it as dd/mm/yyyy is there anyway to make this work with the date in the dd/mm/yyyy format?

I changed it slightly to this:

$ts = strtotime(str_replace('/', '-', $date));

and it now adds it int he right format.

Thanks