Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/409.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何将日期格式从javascript转换为PHP_Javascript_Php_Jquery_Laravel 6 - Fatal编程技术网

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

如何将日期格式从javascript转换为PHP,javascript,php,jquery,laravel-6,Javascript,Php,Jquery,Laravel 6,我在这里有些困惑,当我被要求将javascript中的日期格式转换为php时,我在javascript控制台中的日期不正确,结果是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.getMont

我在这里有些困惑,当我被要求将
javascript
中的日期格式转换为
php
时,我在
javascript
控制台中的日期不正确,结果是
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)
    }
})
// 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("-");
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);
}

如果我的解释无法理解,我道歉,您可以再问我一次,谢谢

我已经用另一种方法解决了我的问题,我对这个问题感到困惑,我将我的代码风格改为:

首先,我将变量放入一个数组中,然后使用
-
连接器使它们
join()
,如下
(app.js)


我想对所有想帮助我解决问题的人说声谢谢,我希望我的代码易于理解并对其他人有用,快乐美好的代码对我来说似乎是一个局部问题。如果我使用您的php函数并直接传递字符串,则一切正常。在操作任何东西之前,请在php函数中回显传递的日期,并查看输出结果。结果是这样的
“7\/1\/2020”(m-d-Y)
hm对我来说似乎很奇怪。因为如果我看得正确,日期似乎是正确的,而且月份/日期也很好。同样,我对这个问题不太了解,因为这个问题真的让我感到困惑,哦,我可以将交换日期格式从
javascript
更改为
日期('Y-d-m')
?但我担心下一步会遇到问题:)为什么要在没有时区信息的情况下将本地时间传递到服务器?我认为新的日期(…)/1000时间戳更好。
public function getMyFunction(Request $list) {
    $firstDate = $list->firstDate;

    echo json_encode($fix_firstDate);
}