Home > database >  How to get this format moment().format('YYYY-MM-DD\THH:mm:ssZ') javascript in php?
How to get this format moment().format('YYYY-MM-DD\THH:mm:ssZ') javascript in php?

Time:09-23

I wish to achieve this same date format from javascript in php:

moment().format('YYYY-MM-DD\THH:mm:ssZ');

output: 2016-12-24T13:46:43-05:00

I'm trying to get the same result, but I've only gotten the following:

$date = new DateTime();
echo $date->format('Y-m-d\TH:i:s-ssZ');

output: 2022-09-22T14:42:28-28280

I don't know if the procedure is correct, but I want to get the same javascript date format but in PHP, what changes should I add to my code?

CodePudding user response:

The procedure is correct, you just need different formatting tokens. The documentation for DateTime::format lists what you can use.

What you can use in this case is P - the documentation says:

Difference to Greenwich time (GMT) with colon between hours and minutes Example: 02:00

echo $date->format('Y-m-d\TH:i:sP');

Demo: https://3v4l.org/PjI5m

N.B. s is for seconds, it has nothing to do with the timezone offset, so I'm not really sure why you used that, especially when you'd already used it to display seconds a bit earlier in the same formatting string (thus making it clear that is has a different purpose). Keep in mind that PHP's date formatting tokens are not the same as the ones used by momentJS.

  • Related