Skip to content Skip to sidebar Skip to footer

How To Handle Datetime Between Php (laravel Api) And Javascript (angularjs)

I'm utterly stuck trying to make php api exchange dates with angular frontend. From PHP to JS I seem to have it sorted. Since Laravel handles dates through Carbon I just added \Car

Solution 1:

$dt = Carbon::now();
echo$dt->toW3cString();       // 2015-02-05T14:50:55+01:00

since carbon can print it it can also parse this format

Carbon::parse('2015-02-05T14:50:55+01:00');

in javascript with moment.js momentjs.com

moment().format(); // 2015-02-05T14:50:55+01:00

Solution 2:

1) make a Carbon date in Laravel controller

$date_from = Carbon::now();
return view('reports', compact('date_from'));

2) You need to init Date in Angular conctoller

rrApp.controller('reportsCtrl', ['$scope', '$filter', function($scope, $filter)
{
        $scope.start_date = new Date("{{$date_from}}");
        $scope.start_date = new Date($filter("date")($scope.start_date, 'yyyy-MM-dd'));// only date, no time

}]); 

3) use it

<inputtype="date"class="form-control" ng-model="start_date">

4) Do NOT try to init the variable in ng-init, it would not work:

Post a Comment for "How To Handle Datetime Between Php (laravel Api) And Javascript (angularjs)"