将时间字符串存储到 Laravel 中的 TIME 列

Storing time strings to TIME column in Laravel

我使用时间选择器来获取时间字符串(例如:“1:00am”或“12:00pm”)。我需要将其修改并存储在 mysql TIME 数据类型列中,例如:'12:00:00'。现在,由于 TIME 列以 24 小时格式存储,我需要 "modify" 它。我似乎找不到使用 Carbon 进行修改的任何帮助,因为我没有使用任何日期。我在 Laravel 中使用了一个突变器,但想知道是否有更好的方法,比如对日期使用 Carbon

public function setTimeAttribute($time) {
    if(substr($time, -2, 0) == 'am') {
        // do nothing
    } else if(substr($time, -2, 0) == 'pm') {
        // increase hours.ie: 1:00pm to 13:00:00
    }
 }

如有任何帮助,我们将不胜感激!谢谢

使用PHPstrtotime()函数。

$time = "12:00am";

$timestamp = strtotime($time);

echo $timestamp;

//Will print something like: 1469228400

或您喜欢的任何其他日期格式。

最好将时间存储为时间戳,要将其转换回您可以使用 date() 函数。 例如:$value = date("H:i:s", $timestamp);