1

I have a php string which contains the date and time as shown below

2015-05-27 18:45:31

How can I convert this to a type which I can insert into postgresql table with timestamp data type?

2 Answers 2

2

Convert it using strtotime() function:

$unix_timestamp=strtotime('2015-05-27 18:45:31');

Thes use $unix_timestamp in your SQL query.

Also you can use TIMESTAMP '2015-05-27 18:45:31' notation right in SQL statement.

Sign up to request clarification or add additional context in comments.

Comments

0

If you have another format of date, you can use DateTime::createFromFormat

In your case:

$dateString = '2015-05-27 18:45:31';
$format = 'Y-m-d H:i:s';
$dateConvert = DateTime::createFromFormat($format, $dateString);
echo "Date: $format; " . $dateConvert->format('Y-m-d H:i:s') . "\n";

The output would be something like:

Date: Y-m-d H:i:s; 2015-05-27 18:45:31

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.