Sobin Augustine
Sobin Augustine

Reputation: 3765

Changing the datetime format in php

Here is the format

'd-m-Y H:i:s'(15-11-2008  7:16:09)

I want to change to this format

'Y-m-d H:i:s' (2008-11-15 07:16:09)

Tried the strtotime() function, but it takes the 'm' as 'd' and 'd' as 'm' Help! new to php..

Current code`

$dt = strtotime($this->input->post('insert_dts'));
$formated_date_time = date("Y-m-d H:i:s",$dt);`

Upvotes: 2

Views: 288

Answers (4)

som
som

Reputation: 4656

As per your code :

$dt = strtotime('15-11-2008 7:16:09');
echo $formated_date_time = date("Y-m-d H:i:s",$dt);

Output :

2008-11-15 07:16:09

Upvotes: 0

Glavić
Glavić

Reputation: 43552

Like @middaparka said in the comment, please use DateTime::createFromFormat.

$date = DateTime::createFromFormat('d-m-Y H:i:s', '15-11-2008 7:16:09');
echo $date->format('Y-m-d H:i:s'); // output is: 2008-11-15 07:16:09

Upvotes: 1

Locohost
Locohost

Reputation: 1702

I use these two methods in my base class(s) all the time...

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");    
}

Let me know if these work out for you :-)

Upvotes: 0

웃웃웃웃웃
웃웃웃웃웃

Reputation: 11984

Try this

$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));

Upvotes: 0

Related Questions