简体   繁体   English

PHP - 将日期转换为YYYY-MM-DDTHH:MM:SS

[英]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: 保留“T”非常重要,如下所示:

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. 我无法弄清楚如何在它们之间添加“T”。

T is a format character so you can't use it directly. T格式字符,因此您无法直接使用它。 Escape it ( \\T ) to get a literal T character: 转义它( \\T )以获得文字T字符:

http://php.net/manual/en/function.date.php 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');
$invoice_date = date('Y-m-d\TH:i:s');

You can create an object of the DateTime and set the Timezone . 您可以创建DateTime的对象并设置时区 You can see a list of Timezone strings here. 您可以在此处查看时区字符串列表。 http://php.net/manual/en/timezones.php 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 希望这可以帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM