gpt4 book ai didi

php - 可以更改日期时间本地输入的格式

转载 作者:行者123 更新时间:2023-11-29 10:02:34 24 4
gpt4 key购买 nike

NOTE: This question is not a duplicate, the reason is that I don't use only PHP, It's true that my pages are elaborated in PHP but with code of HTML.

我会再重复一遍,当然我使用 php,但我在 PHP 文件中使用 HTML;我正在使用 laravel 框架,并且我也使用 Blade,我从提到的页面中放置了链接。

Changing the datetime format in php

现在,我继续解释我的问题。

我有一个表单输入,其类型是 datetime-local,输入以这种格式显示日期:

年-月-日 00:00

但是在我的数据库中日期时间的格式是:

yyyy-mm-dd 00:00:00

我已经知道这个日期时间格式与我的时区配置有关,但如果可能的话,我想更改格式,直观地更改格式并保存为该格式,就像在我的数据库中一样。我只讨论日期,不讨论时间。

我认为如果可能的话我的数据库不会出现问题。

如果可能的话,我想使用 javascript 或在我的函数中更改格式 save()我的 Controller ,创建一种更改格式的方法。我的数据库在mysql中,我在phpmyadmin中工作。

我还把我的函数 store()我的 Controller 。

public function store(Request $request){


$hours = new HoursNew();

try {
/*THE NAME FROM THE INPUTS IN MY FORM,
ALSO THE NAMES OF THE FIELDS OF MY TABLE OF MY DATABASE*/

$hours->id = $request->id;
$hours->time_start = $request->time_start;
$hours->time_end = $request->time_end;
$hours->estate_time_id = $request->estate_time_id;
$hours->court_id = $request->court_id;
$hours->save();


} catch (\Illuminate\Database\QueryException $e) {

Session::flash('error', 'Ups! We have some problems to process your operation');

return redirect()->route('ListHours.store');

}

Session::flash('message', "It's OK");
return redirect()->route('ListHours.store');

}

注意:对我的数据库不是很重要,但了解它很重要:

将秒保存到我的数据库中并不是很重要。

最佳答案

使用 Carbon 来做到这一点。

Carbon::parse('your date')->toDateTimeString() 应该可以完成这项工作。

此外,Carbon 可以使用解析静态方法创建实例,如下所示:

Carbon::createFromFormat('格式','您的数据')->toDateTimeString()

关于php - 可以更改日期时间本地输入的格式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52616164/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com