php - How to parse date string to dateTime in php?
问题描述
I have a date string in this format "2014-12-01 16:00:02,153". How do I parse this to a datetime format for mysql?
I am using laravel and Carbon. My code is Carbon::parse("2014-12-01 16:00:02,153");
but get an error that reads DateTime::__construct(): Failed to parse time string (2014-12-01 16:00:02,153) at position 20 (1): Unexpected character
解决方案
You could use the createFromFormat method from the DateTime object :
$dateTime = DateTime::createFromFormat('Y-m-d H:i:s,u', "2014-12-01 16:00:02,153");
echo $dateTime->format('Y-m-d H:i:s');
推荐阅读
- angular - 下拉列表的角度更改事件不起作用
- c# - 我可以从模型类生成表模式吗?
- python - 当我尝试导入我所做的模块时未找到模块错误
- python - 是否可以使用 Django 在同一个视图中读取和更新?
- rust - 如何将 wasm 特定的宏与函数和结构分离
- azure-devops - Databricks API 2.0 - 无法使用 SPN 凭据创建 KEYVAULT 机密范围
- qt - 如何创建能够嵌套其他组件的组件
- github - GitHub 工作流程中的条件运行步骤
- node.js - package.json - 禁止在包源中使用某些文件名
- python - 将从 API 接收到的 png 格式文本转换为 png 文件 - python