We help IT Professionals succeed at work.
Get Started

PHP Date Formatting

Last Modified: 2019-11-23
I am using PHP v 5.6.25 and MS SQL Server 2014. I have a column that is defined as a datetime. I am extracting data and this is what the data looks like that I am trying to format and insert into this column:

 Wed, 20 Nov 2019 18:21:05 -0500

Open in new window

I was able to extract the following:

20 Nov 2019 18:21:05

Open in new window

 and then was able to turn it into this:

 11/20/2019 18:21:05

Open in new window

But I need it in 12 hr format and I know the format needs to be this (I think)

$date = new DateTime($wrkRecvDate);

Open in new window

The above produces this:

2019/20/11 6:06:21:05

Open in new window

which is exactly what I wanted HOWEVER it will not insert into my
table - I have attached a image of the error message.
 I am using a sql insert statement and I have tried with/without enclosed with quotes and it will not work.
If I just use the date portion and drop the time, the record will insert just fine.
I have also tried this as to formatting:
$date = new DateTime($wrkRecvDate);
 echo $date->format('Y/d/m   g:h:i:s')

Open in new window

which produced this:2019/20/11 6:06:21:05 - again, exactly what I needed but will not insert into the column.
Any help would be appericiated.

Watch Question
The More I know, the more I don't know
Distinguished Expert 2019
This problem has been solved!
Unlock 1 Answer and 15 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant

An Experts Exchange subscription includes unlimited access to online courses.

Get Started
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE