oliverbj
oliverbj

Reputation: 6052

PHP - Convert date to YYYY-MM-DDTHH:MM:SS

I was wondering if it was possible to format todays date to below format:

YYYY-MM-DDTHH:MM:SS

It's important that the "T" is preserved, like this:

2017-07-20T00:00:00

Below I have:

$invoice_date = date('Y-m-d H:i:s');

I can't figure out how to add the "T" in between.

Upvotes: 7

Views: 29235

Answers (3)

Oluwaseye
Oluwaseye

Reputation: 695

You can create an object of the DateTime and set the Timezone. You can see a list of Timezone strings here. http://php.net/manual/en/timezones.php

$invoice_date =  (new \DateTime('America/New_York'))->format('Y-m-d\TH:i:s');

echo $invoice_date;

Hope this helps

Upvotes: 2

Halcyon
Halcyon

Reputation: 57709

T is a format character so you can't use it directly. Escape it (\T) to get a literal T character:

http://php.net/manual/en/function.date.php

You can prevent a recognized character in the format string from being expanded by escaping it with a preceding backslash.

$invoice_date = date('Y-m-d\TH:i:s');

Upvotes: 37

buildok
buildok

Reputation: 785

$invoice_date = date('Y-m-d\TH:i:s');

Upvotes: 5

Related Questions