5

I have a string in this format:

Thu, 26 Feb 2015 11:39:59

And I wonder if its possible to transform it to a valid timestamp format in order to insert it in my PostgreSQL database.

So ideally in the end I would have something like:

2015-03-26 11:39:59

Is there a function in PHP to do something like this?

2 Answers 2

9

Use DateTime() to format date string.

$date = 'Thu, 26 Feb 2015 11:39:59';

$date = new DateTime($date);

echo $date->format('Y-m-d H:i:s');
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks. That worked. I was not aware of this function and I was ready to do it with a much more complicated way.
Sidenote: to get the current date formatted as input to DateTime, use date("Y-m-d h:m:s")
2

You can also use it as DateTime::createFromFormat

$date = 'Thu, 26 Feb 2015 11:39:59';
$date = DateTime::createFromFormat('D, d M Y H:i:s', $date);
echo $date->format('Y-m-d H:i:s'); //2015-03-26 11:39:59

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.