dourao3960 2013-06-19 14:52
浏览 66
已采纳

PHP time()函数返回epoch而不是当前时间

I have the following php code that is meant to format the current date and time:

$rawdatetime = time();
$datetime = date('Y-m-d', $rawdatetime) . 'T' . date('H:i:s', $rawdatetime) . '.000Z';
$this->debug($datetime);

The formatting seems to be working fine, but it keeps outputting that it is 1970; I get the following output:

1970-01-01T00:00:00.000Z

My guess is that my server is not configured properly, but my Google search has not given me any clues. I am running WAMP if it helps.

Thanks in advance for any advice you may have.

EDIT: it seems that the date and time functions are working properly; but assigning them to a variable is what is the problem. Any work arounds to get the same formatting as above would be welcomed. But I would also like to know why this problem is happening.

  • 写回答

2条回答 默认 最新

  • douyong5476 2013-06-19 15:20
    关注

    Firstly, you don't need to use time() at all here, because date() will use the current time as the default value if you don't pass a value to that parameter.

    Secondly, you're using two separate date() calls, separated by a "T". Note that the formatting for date() can accept a hard-coded character like T; you just need to escape them with backslashes, so you don't need to split it into two function calls.

    Your entire code could look like this:

    $datetime = date('Y-m-d \T H:i:s.\0\0\0\Z');
    

    Which gives 2013-06-19 T 11:18:53.000Z

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 unity第一人称射击小游戏,有demo,在原脚本的基础上进行修改以达到要求
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)