在php中更改日期时间格式
这是格式在php中更改日期时间格式,php,Php,这是格式 'd-m-Y H:i:s'(15-11-2008 7:16:09) $dt = strtotime($this->input->post('insert_dts')); $formated_date_time = date("Y-m-d H:i:s",$dt);` 我想换成这种格式 'Y-m-d H:i:s' (2008-11-15 07:16:09) $dt = strtotime($this->input->post('insert_dts'));
'd-m-Y H:i:s'(15-11-2008 7:16:09)
$dt = strtotime($this->input->post('insert_dts'));
$formated_date_time = date("Y-m-d H:i:s",$dt);`
我想换成这种格式
'Y-m-d H:i:s' (2008-11-15 07:16:09)
$dt = strtotime($this->input->post('insert_dts'));
$formated_date_time = date("Y-m-d H:i:s",$dt);`
尝试了strotime()函数,但它将'm'作为'd','d'作为'm'
救命啊!php新手
$dt = strtotime($this->input->post('insert_dts'));
$formated_date_time = date("Y-m-d H:i:s",$dt);`
当前代码`
$dt = strtotime($this->input->post('insert_dts'));
$formated_date_time = date("Y-m-d H:i:s",$dt);`
试试这个
$dt = strtotime($this->input->post('insert_dts'));
$formated_date_time = date("Y-m-d H:i:s",$dt);`
$str = date('d-m-Y H:i:s',strtotime('15-11-2008 7:16:09'));
echo date('Y-m-d H:i:s',strtotime($str));
我一直在基类中使用这两种方法
$dt = strtotime($this->input->post('insert_dts'));
$formated_date_time = date("Y-m-d H:i:s",$dt);`
protected static function Now() {
return date_create(date('Y-m-d H:i:s')) ;
}
protected static function NowStr() {
return date_format(self::Now(), "Y-m-d H:i:s");
}
让我知道这些是否适合你:-)如@middaparka在评论中所说,请使用
$dt = strtotime($this->input->post('insert_dts'));
$formated_date_time = date("Y-m-d H:i:s",$dt);`
根据您的代码:
$dt = strtotime($this->input->post('insert_dts'));
$formated_date_time = date("Y-m-d H:i:s",$dt);`
$dt = strtotime('15-11-2008 7:16:09');
echo $formated_date_time = date("Y-m-d H:i:s",$dt);
输出:
$dt = strtotime($this->input->post('insert_dts'));
$formated_date_time = date("Y-m-d H:i:s",$dt);`
2008-11-15 07:16:09如果您已经知道日期的格式,则无需使用strotime-而是使用类似解析现有日期的函数。函数Now()可以轻松地
返回新的日期时间代码>。函数NowStr()可以是返回self::Now()->格式('Y-m-dh:i:s')代码>@Glavić:是的,你可能是对的,但这将如何改变/改进功能?这只是我的拙见,我更喜欢面向对象的风格而不是过程式风格。示例几乎相同,但由于使用了date()
函数,您的函数Now()
在2038年(2038年1月19日03:14:07 GMT)后将无法工作。若您查看DateTime构造函数(),您将看到第一个参数具有默认值“now”,所以若您需要当前时间,只需调用不带参数的函数,如return date_create()代码>@Glavić:明白了。我只是想知道我是否有一个bug或者它很慢或者类似的东西:-)@SobinAugustine你可以这样做str_replace('/','-',yourdatevariable)
*SobinAugustine:如果这对你不起作用,你为什么接受这个答案*som:开始使用DateTime类;您的示例不适用于2038年之后的日期。@Glavic在这样做之后工作,str_replace(“/”,“-”,yourdatevariable)
$dt = strtotime($this->input->post('insert_dts'));
$formated_date_time = date("Y-m-d H:i:s",$dt);`