从datepicker到数据库的故障保存故障

发布于 2025-02-12 23:09:56 字数 623 浏览 4 评论 0原文

目前,我有一个jQuery DatePicker,可以从日期和迄今为止添加一个。两个日期都从相同的输入中添加。我最终得到的格式是

$request->input('datepicker') // 07/29/2022 - 07/30/2022

我在日期案例中有一个start_at日期,这是时间戳,

这是我试图在保存到数据库之前试图更改格式的方式,

$firstDate = Carbon::parse(strtok($request->input('daterange'), '-'));

$startsAt = Carbon::createFromFormat('m/d/Y', $firstDate)->format('y-m-d');

则会给我以下错误

The separation symbol could not be found Unexpected data found. Trailing data

如果我删除碳, :: parse()我认为首先不是nessesary,而错误只是

Trailing data

由同一行引起的

Currently I have a jquery datepicker where it is possible to add a from date and to date. Both of the dates get added from the same input. The format I end up with is this

$request->input('datepicker') // 07/29/2022 - 07/30/2022

I have a starts_at date in the datebase which is a timestamp

This is how I am trying to change the format before saving it to the database

$firstDate = Carbon::parse(strtok($request->input('daterange'), '-'));

$startsAt = Carbon::createFromFormat('m/d/Y', $firstDate)->format('y-m-d');

The startsAt line is giving me the following error

The separation symbol could not be found Unexpected data found. Trailing data

If I remove the Carbon::parse() which I thought first was not nessesary the error is instead just

Trailing data

Caused by the same line

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

很快妥协 2025-02-19 23:09:56
$dates = array_map('trim', explode('-', $request->input('datepicker'))); // return array ['07/29/2022', '07/30/2022'] trim to remove whitespaces at the start and end.

然后:

$startsAt = Carbon::createFromFormat('m/d/Y', $dates[0])->format('y-m-d');
$endsAt = Carbon::createFromFormat('m/d/Y', $dates[1])->format('y-m-d');

只需要确保输入始终采用该格式,因此您可能需要一个验证REGEXP,以确保其始终为“ M/d/y -m/y -m/d/y”格式

$dates = array_map('trim', explode('-', $request->input('datepicker'))); // return array ['07/29/2022', '07/30/2022'] trim to remove whitespaces at the start and end.

Then:

$startsAt = Carbon::createFromFormat('m/d/Y', $dates[0])->format('y-m-d');
$endsAt = Carbon::createFromFormat('m/d/Y', $dates[1])->format('y-m-d');

Just you need to be sure that the input is always on that format, so you probably need a validation regexp to ensure that always comes as "m/d/Y - m/d/Y" format

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文