dsuw85815
2017-07-20 14:09
浏览 364
已采纳

PHP - 将日期转换为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.

图片转代码服务由CSDN问答提供 功能建议

我想知道是否可以将今天的日期格式化为以下格式: < pre> YYYY-MM-DDTHH:MM:SS

保留“T”非常重要,如下所示: <前> 2017-07-20T00:00:00

下面我有:

 <  code> $ invoice_date = date('Ymd H:i:s'); 
   
 
 

我无法弄清楚如何在它们之间添加“T”。

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

3条回答 默认 最新

  • doutui6241 2017-07-20 14:12
    已采纳

    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');
    
    打赏 评论
  • duanfei1268 2017-07-20 14:11
    $invoice_date = date('Y-m-d\TH:i:s');
    
    打赏 评论
  • duangeli1334 2017-07-20 14:45

    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

    打赏 评论

相关推荐 更多相似问题