Jack hardcastle Jack hardcastle - 1 year ago 128
PHP Question

Create DateTime from date string and time string

I have a form that submits both a date and a time, and I wish to create one \DateTime object based on these values


value looks like: 'T09:45:00'

value looks like: '2016-07-11'

I've tried

'Y-m-d TH:i:s', $reportArray['date'] . ' ' . $reportArray['time'])
); // also tried without 'T' (TH:i:s)

However this broke the script.

Is there a simple way to create one \DateTime object from one date string and one time string?

Answer Source

The 'T' you have used in the format represents a time zone, not a literal 'T'. You could use this format: 'Y-m-d\TH:i:s'. The backslash escapes the 'T' to a literal 'T'. I left out the space, because you don't need it. Like this:

    'Y-m-d\TH:i:s', $reportArray['date'] . $reportArray['time'])

This answer does, of course, assume that the 'T' is always there and never changes.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download