I have some confused in here, when I was made a method to convert date format in javascript to php, my date is not right when in javascript console result is 2020/7/1 but in my php result is 2020-01-07 when I use date('Y-m-d') method. For full script I put in below for more continous understanding to my problem :
my app.js :
// get today
const today = new Date();
// get month of today
const todayInMonth = today.getMonth() + 1;
// get year of today
const todayInYear = today.getFullYear();
// count date in one month (today)
const countDayInMonth = new Date(todayInYear, todayInMonth, 0).getDate();
// get number 15 of date
let dividerOne;
countDayInMonth == '31' ? dividerOne = Math.floor(countDayInMonth / 2) : countDayInMonth == '30' ? dividerOne = countDayInMonth / 2 : countDayInMonth == '29' ? dividerOne = Math.round(countDayInMonth / 2) : dividerOne = Math.ceil(countDayInMonth / 2);
// get number 15 / 16 of date
let dividerTwo = countDayInMonth - dividerOne;
// get fist date in every month
let firstDate = new Date(todayInYear, todayInMonth - 1, 1).toLocaleDateString();
$.ajax({
url: "myController/getMyFunction",
method: "POST",
data: {firstDate: firstDate},
async: true,
dataType: "json",
success: function (data) {
console.log(data)
}
})
my myController.php :
public function getMyFunction(Request $list) {
$firstDate = $list->firstDate;
//change '/' to '-' and read to YYYY-MM-DD
$raw_firstDate = strtr($firstDate, '/', '-');
$fix_firstDate = date('Y-m-d', strtotime($raw_firstDate));
echo json_encode($fix_firstDate);
}
If my explanation is incomprehensible, I apologize, and you can ask me again, Thank You