如何将日期格式从 javascript 转换为 PHP

How to convert date format from javascript to PHP

我在这里有些困惑,当我得到一个将 javascript 中的日期格式转换为 php 的方法时,我的日期在 javascript [=17] 中不正确=] 结果是 2020/7/1 但在我的 php 中,当我使用 date('Y-m-d') 方法时,结果是 2020-01-07。对于我在下面放置的完整脚本,以便更持续地了解我的问题:

我的 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)
    }
})

我的 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);
}

如果我解释的不明白,请见谅,您可以再问我,谢谢

我已经用另一种方法解决了我的问题,我要解决这个问题并将我的代码风格更改为:

首先我把我的变量变成一个数组,然后用 - 连接器把它们变成 join(),像这样 (app.js) :

// get today
const today = new Date();
// get year of today
const todayInYear = today.getFullYear();
// get month of today
let todayInMonth = '' + today.getMonth();
todayInMonth.length < 2 ? todayInMonth = '0' + (today.getMonth() + 1) : todayInMonth = today.getMonth() + 1;
// count date in one month (today)
const countDayInMonth = new Date(todayInYear, todayInMonth, 0).getDate().toString();
// 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 = [todayInYear, todayInMonth, "0" + 1].join("-");
// get date range 1 - 15
let rangeOne = [todayInYear, todayInMonth, dividerOne].join("-");
// get date range 15 / 16 - end
let rangeTwo = [todayInYear, todayInMonth, dividerTwo].join("-");
// get last date in every month
let lastDate = [todayInYear, todayInMonth, countDayInMonth].join("-");

对于我在 controller 中的代码,我只是从我的 js 代码中调用我的数据,就像这样:

public function getMyFunction(Request $list) {
    $firstDate = $list->firstDate;

    echo json_encode($fix_firstDate);
}

我想对所有想帮助我解决问题的人说声谢谢,我希望我的代码易于理解并且对其他人有用,快乐的好代码