最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript full text Date() format with PHP carbon - Stack Overflow

programmeradmin9浏览0评论

I am generating and sending full date string from javascript Date() function which returns full string date format like this:

Sun Jan 01 2017 00:00:00 GMT+0100 (Central European Standard Time)

Carbon parser wont accept this format for creating the same date on server side. This does not work:

$date = Carbon::parse('Sun Jan 01 2017 00:00:00 GMT+0100 (Central European Standard Time)');

Error Failed to parse time string (Sun Jan 01 2017 00:00:00 GMT+0100 (Central European Standard Time)) at position 41 (l): Double timezone specification

If I remove (Central European Standard Time) works:

$date = Carbon::parse('Sun Jan 01 2017 00:00:00 GMT+0100');

Then it correctly creates date.

Can JS default Date() be used in Carbon somehow or will I have to format date before sending it to Carbon?

I am generating and sending full date string from javascript Date() function which returns full string date format like this:

Sun Jan 01 2017 00:00:00 GMT+0100 (Central European Standard Time)

Carbon parser wont accept this format for creating the same date on server side. This does not work:

$date = Carbon::parse('Sun Jan 01 2017 00:00:00 GMT+0100 (Central European Standard Time)');

Error Failed to parse time string (Sun Jan 01 2017 00:00:00 GMT+0100 (Central European Standard Time)) at position 41 (l): Double timezone specification

If I remove (Central European Standard Time) works:

$date = Carbon::parse('Sun Jan 01 2017 00:00:00 GMT+0100');

Then it correctly creates date.

Can JS default Date() be used in Carbon somehow or will I have to format date before sending it to Carbon?

Share Improve this question asked Nov 15, 2018 at 13:40 Primoz RomePrimoz Rome 11.1k19 gold badges83 silver badges113 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 7

Carbon extends PHP's native DateTime class, so you can use createFromFormat instead:

$date = 'Sun Jan 01 2017 00:00:00 GMT+0100 (Central European Standard Time)';
$carbon = Carbon::createFromFormat('D M d Y H:i:s e+', $date);

The important part of the format specification is the + at the end, which tells it to ignore any trailing data.

See https://3v4l/Rnen7 for a demo (using DateTime rather than Carbon)

You can pass the date in ISO format, Carbon understands ISO format. You can get the date in ISO format using new Date().toISOString()

发布评论

评论列表(0)

  1. 暂无评论