2014-08-31 96 views

回答

0

$query = DB::query("select concat(date," ",time) as datetime from TABLENAME");使用此行查询,您可以连接日期和时间DB值;它会像日期时间值一样工作。

0

您可以使用增变了这一点:

// assuming your date field is in format Y-m-d and time H:i:s 
// and you want the property named someTimestamp 

public function getSomeTimestampAttribute() 
{ 
    // use Carbon\Carbon for the class 
    return new Carbon($this->attributes['date'] . ' ' . $this->attributes['time']); 
} 

public function setSomeTimestampAttribute($dateTime) 
{ 
    // parse param to get string 'Y-m-d H:i:s' 
    $dateTime = $this->fromDateTime($dateTime); 

    $date = substr($dateTime, 0, 10); // Y-m-d 
    $time = substr($dateTime, 11); // H:i:s 

    $this->attributes['date'] = $date; 
    $this->attributes['time'] = $time; 
} 

然后,你可以这样做:

// get 
$model->someTimestamp; // Carbon instance depending on date & time db fields 
$model->someTimestamp->diff(Carbon::now()); 

// set - all methods do the same 
$model->someTimestamp = new Carbon('2014-08-31 11:55:00'); 
$model->someTimestamp = new DateTime('2014-08-31 11:55:00'); 
$model->someTimestamp = '2014-08-31 11:55:00'; 

// then this is true 
$model->date; // 2014-08-31 
$model->time; // 11:55:00 
// so you can now call ->save() on the model and values in db will be correct 
0

串联两个变量存储是不够的,它需要更多。我使用Laravel,所以在我的情况下,这并获得成功:

$datetime = $date.' '.$time; 
    $datetime = new DateTime($datetime); 

所有我需要的是一个DateTime对象,这是我得到了它。因此,在此之后,我可以轻松获得两个DateTime对象之间的区别。

$currentDateTime = new DateTime(); //Returns current time & date in a DateTime object 
    $difference = $currentDateTime->diff($datetime); 

$ difference有所有我想要的。

相关问题