我的DTO类中有一个字段,它接受start_time和end_time作为“2:00AM”。
/**
* @var string
*/
#[CastWith(TimeCaster::class)]
public string $start_time; // 01:00 AM example
/**
* @var string
*/
#[CastWith(TimeCaster::class)]
public string $end_time;我可以在我的Caster类中使用Carbon来解析这种格式的时间吗
#[\Attribute] class TimeCaster implements Caster
{
public function cast(mixed $value): mixed
{
return Carbon::parse($value)->format();
}
}发布于 2021-06-26 18:48:52
我想你用的是Carbon::createFromFormat
Carbon::createFromFormat('H:i A','10:00 PM')->format('Y-m-d H:i:s)如果您只想使用时间戳来获取时间,那么
Carbon::parse("2021-06-26 22:00:00")->format('g:i A')https://stackoverflow.com/questions/68141554
复制相似问题