Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert javascript date to Carbon Date?

Tags:

date

php

laravel

I need to convert javascript date to Carbon date. i want to filter records by month and year Only.

 $date  = Javascript Date to Carbon Date? 
 $logs =  $logs->whereBetween('created_at', [$date->startOfMonth(), $date->endOfMonth()]); 

Thanks in Advance.

like image 672
Neha Avatar asked Jan 21 '19 10:01

Neha


2 Answers

Hard to guess without the JS date output but you can try with

Carbon::parse($date);

If it fails you need to format you JS date and then use

Carbon::createFromFormat('Y-m-d H:i:s', $date);
like image 181
lovis91 Avatar answered Sep 19 '22 19:09

lovis91


You can use this approach where new Date() is like Sat Aug 24 2019 20:36:26 GMT+0600 (Bangladesh Standard Time)

first use following JavaScript function for parsing new Date()

   function datefilter(date){
            let month = date.getMonth();
            let year = date.getFullYear();
            let day = date.getDate();
            return day+'-'+month + '-' + year;
    }
    datafilter(new Date());

The output will be "24-7-2019"

then use Carbon createFromFormate like

Carbon::createFromFormat('d-m-Y', $data['enddate']);

I guess output will remain to same"24-7-2019"

like image 22
ANIK ISLAM SHOJIB Avatar answered Sep 22 '22 19:09

ANIK ISLAM SHOJIB