简体   繁体   中英

How to convert date format from javascript to PHP

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('Ym-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

I already solved my problem with another way, I'm to stuck about this problem and I change my style of code to this:

first of all I make my variable into an array, then make them join() with - connector, like this (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("-");

and for my code in controller I just called my data from my js code, like this:

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

    echo json_encode($fix_firstDate);
}

I wanna say thanks to everyone want to help me solve my problem, I hope my code will be easy to understand and can be useful for others, happy nice code

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM