Reputation: 373
I am using fullcalendar jquery plugin for my page.When i'm inserting new events using the fullcalendar plugin.., its returning me epoch time values instead of UTC timedate values.
Below is the code that inserts new data into the database on clicking a date.
calendar.fullCalendar({
header: {
left: 'prev,next today',
center: 'title',
right: 'month,agendaWeek,agendaDay'
},
editable: true,
droppable: true, // this allows things to be dropped onto the calendar
drop: function() {
// is the "remove after drop" checkbox checked?
if ($('#drop-remove').is(':checked')) {
// if so, remove the element from the "Draggable Events" list
$(this).remove();
}
},
eventSources: [
{
url: '/v1/calendar/',
type: 'GET',
dataType:'json',
},
calendar.fullCalendar( 'addEventSource', response )
],
selectable: true,
selectHelper: true,
select: function(start, end, allDay) {
bootbox.prompt("New Event Title:", function(title) {
var people_id=1;
//var title=event.title;
//var start=event.start;
//var end=event.end;
if (title !== null) {
calendar.fullCalendar('renderEvent',
{
people_id:people_id,
title: title,
start: start,
end: end,
allDay: allDay
},
true // make the event "stick"
);
$.ajax({
url: '/v1/calendar',
data: 'people_id='+people_id+'&title='+title+'&start='+start+'&end='+end,
type: 'POST',
dataType: 'json',
success: function(response){
bootbox.alert("Event Created!");
console.log(response);
},
error: function(e){
console.log(e.responseText);
}
});
}
});
The event is successfully added into the database...but the time is in epoch format.
the console response I'm getting is given below:
{people_id: "1", evt_description: "testing", date1: "1431388800000", date2: "1431475200000", event_id: 4}
I'm using laravel framework at the backend I'm attaching my CalendarController below:
<?php
class CalendarController extends \BaseController {
/**
* Display a listing of calendar
*
* @return Response
*/
public function index()
{
$event = DB::table('events')
->leftJoin('people','people.people_id','=','events.people_id')
->leftJoin('people_roles','people_roles.people_id','=','events.people_id')
->get(array('events.people_id','events.event_id','events.evt_description','events.date1','events.date2','events.time'));
//return View::make('people.show', compact('address'));
//return Response::json($event);
$id=array();
$title=array();
$start=array();
$end=array();
$i=0;
foreach ($event as $events)
{
$id[$i]=$events->event_id;
$title[$i]=$events->evt_description;
$start[$i]=$events->date1;
$end[$i]=$events->date2;
$i++;
}
return Response::json(array('id'=>$id,'title'=>$title,'start'=>$start,'end'=>$end));
}
/**
* Show the form for creating a new calendar
*
* @return Response
*/
public function create()
{
return View::make('calendar.create');
}
/**
* Store a newly created calendar in storage.
*
* @return Response
*/
public function store()
{
$events= Input::get('type');
$events= new Events;
$events->people_id = Input::get('people_id');
$events->evt_description =Input::get('title');
$events->date1 =Input::get('start');
$events->date2 =Input::get('end');
//$events->time =Input::get('time');
$events->save();
return Response::json($events);
//return Redirect::route('calendar.index');
}
/**
* Display the specified calendar.
*
* @param int $id
* @return Response
*/
public function show($id)
{
$calendar = Calendar::findOrFail($id);
return View::make('calendar.show', compact('calendar'));
}
/**
* Show the form for editing the specified calendar.
*
* @param int $id
* @return Response
*/
public function edit($id)
{
$calendar = Calendar::find($id);
return View::make('calendar.edit', compact('calendar'));
}
/**
* Update the specified calendar in storage.
*
* @param int $id
* @return Response
*/
public function update($id)
{
//$type=Input::get('type');
$event_id= Input::get('event_id');
$title= Input::get('title');
$roles = DB::table('events')
->where('event_id','=',$event_id )
->update(array('evt_description' => $title));
return Response::json(array('id'=>$event_id,'title'=>$title));
}
/**
* Remove the specified calendar from storage.
*
* @param int $id
* @return Response
*/
public function destroy()
{
// Calendar::destroy($id);
$event_id= Input::get('eventid');
DB::table('events')->where('event_id','=',$event_id)->delete();
return Response::json($event_id);
// return Redirect::route('calendar.index');
}
}
Upvotes: 0
Views: 580
Reputation: 58
Try this
$newstrt=substr($start_day,0,10);
$newend=substr($end_day,0,10);
$events->start= date("Y-m-d",$newstrt);
$events->end= date("Y-m-d", $newend);
$events->save();
Upvotes: 1
Reputation: 14970
Just use Carbon to treat the dates on your controller. In your store method, instead of
$events->date1 =Input::get('start');
$events->date2 =Input::get('end');
use
$events->date1 = Carbon::createFromTimeStamp(Input::get('start'))->toDateTimeString();
$events->date2 = Carbon::createFromTimeStamp(Input::get('end'))->toDateTimeString();
Make sure to use the package on your Calendar controller. Example:
<?php
use Carbon\Carbon;
class CalendarController extends \BaseController {
There's no need to install it because it's already a dependency of Laravel.
Upvotes: 0