Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

date

php

time

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.

like image 317
oliverbj Avatar asked Jul 20 '17 14:07

oliverbj


People also ask

How to format date to YYYY-mm-DD in php?

Change DD-MM-YYYY to YYYY-MM-DD In the below example, we have date 17-07-2012 in DD-MM-YYYY format, and we will convert this to 2012-07-17 (YYYY-MM-DD) format. $orgDate = "17-07-2012"; $newDate = date("Y-m-d", strtotime($orgDate)); echo "New date format is: ".

How to convert date to strtotime in php?

php $date1 = "2019-05-16"; $timestamp1 = strtotime($date1); echo $timestamp1; // Outputs: 1557964800 $date2 = "16-05-2019"; $timestamp2 = strtotime($date2); echo $timestamp2; // Outputs: 1557964800 $date3 = "16 May 2019"; $timestamp3 = strtotime($date3); echo $timestamp3; // Outputs: 1557964800 ?>

What is T and Z in timestamp php?

P - Difference to Greenwich time (GMT) in hours:minutes (added in PHP 5.1.3) T - Timezone abbreviations (Examples: EST, MDT) Z - Timezone offset in seconds. The offset for timezones west of UTC is negative (-43200 to 50400) c - The ISO-8601 date (e.g. 2013-05-05T16:34:42+00:00)

How to convert YYYY-mm-DD to DD mm YYYY in jQuery?

Re: convert Date from YYYY-MM-DD to MM/DD/YYYY in jQuery/JavaScript. var tempDate = new Date("2021-09-21"); var formattedDate = [tempDate. getMonth() + 1, tempDate. getDate(), tempDate.


3 Answers

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');
like image 83
Halcyon Avatar answered Nov 09 '22 23:11

Halcyon


$invoice_date = date('Y-m-d\TH:i:s');
like image 43
buildok Avatar answered Nov 09 '22 22:11

buildok


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

like image 42
Oluwaseye Avatar answered Nov 10 '22 00:11

Oluwaseye