我使用Laravel非常新,我在迁移中创建的列有问题。我有一个类型为date的列。我的问题是,我不知道如何管理它,如何为表格存储一些数据等。
我问是否有人可以告诉我,在带有参数Resquest $ r的控制器的方法中,如何在日期变量中转换该表单中的字符串。我一直在寻找,但我无法找到任何可以帮助我的东西。提前谢谢!
编辑:
这是我的控制器的一种方法:
public function addObjectives(Request $r,$id){
$gobjectives = Mgobjective::all();
$sobjectives = Msobjective::all();
$mprogram = Mprogram::find($id);
$a = Area::find($mprogram->marea_id);
if($a==null){
$area = new Area();
$area->id = $mprogram->marea_id;
$area->saveArea();
}
$p = Program::find($id);
if($p==null){
$program = new Program();
$program->id = $id;
$program->initialDate= "";
$program->finalDate= "";
$program->frequency= "";
$program->area_id = $mprogram->marea_id;
$program->saveProgram();
}
}
在initialDate和finalDate de program中,我有一个空字符串,因为它之前是一个字符串。我想提出一个不一定是实际日期的日期。
答案 0 :(得分:0)
使用date()
功能,如:
$date = date('Y-m-d', strtotime('your date'));
说明: Y-m-d
是mysql date
类型的默认日期格式。那么你有什么形成日期,把它转换成Y-m-d
。
答案 1 :(得分:0)
<?php
namespace App\Http\Controllers;
class YourController extends Controller
{
public function yourMethod(Request $request)
{
// If you use a HTML date element, you "should" receive the data in yyyy-mm-dd
// format which can be understood by DateTime's constructor
$date = new \DateTime($request->input('date'));
// If you use a different input or expect a date in a different format you can
// use the createFromFormat method, where your first parameter is the date
// format you expect and the second is the date input.
$date = \DateTime::createFromFormat('d/m/Y', $request->input('date'));
// Once your date has been loaded into a DateTime object, you can use the
// format method to output the date in any format you like. MySQL and other
// databases tend to expect and store dates in yyyy-mm-dd format.
echo $date->format('Y-m-d');
}
}
答案 2 :(得分:0)
尝试使用Carbon
Carbon::createFromDate($year, $month, $day, $tz);
或
Carbon::createFromFormat('Y-m-d H', '1975-05-21 22')->toDateTimeString(); // 1975-05-21 22:00:00