dongshan3759 2014-02-03 08:46
浏览 74
已采纳

PHP:将负时间戳转换为日期[关闭]

I have a negative timestamp and I wanted to convert it to a readable date format.

$timestamp = -1861945262080;

If I use date("d-m-Y", $timestamp), it will just output 12-08-2035.

  • 写回答

1条回答 默认 最新

  • douxia6554 2014-02-03 08:51
    关注

    The below piece of code transforms your UNIX timestamp to a valid date-month-year. However , passing pretty large negative unix timestamps can produce unexpected results as shown below.

     <?php
        $dt = new DateTime();
        $dt->setTimestamp(-1861945262080); //<--- Pass a UNIX TimeStamp
        echo $dt->format('d-m-Y');
    

    OUTPUT :

    12-08-2035
    

    However, you still can pass negative timestamps to the above thing. Consider this excerpt from wikipedia.

    The Unix time number is zero at the Unix epoch, and increases by exactly 86400 per day since the epoch. Thus 2004-09-16T00:00:00Z, 12677 days after the epoch, is represented by the Unix time number 12677 × 86400 = 1095292800. This can be extended backwards from the epoch too, using negative numbers; thus 1957-10-04T00:00:00Z, 4472 days before the epoch, is represented by the Unix time number -4472 × 86400 = -386380800.

    So let's pass the -386380800 to the above code.

     <?php
        $dt = new DateTime();
        $dt->setTimestamp(-386380800); //<--- Pass a UNIX TimeStamp
        echo $dt->format('d-m-Y');
    

    OUTPUT :

    04-10-1957
    

    which is the expected output as per the sources.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 lammps拉伸应力应变曲线分析
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥15 请问Lammps做复合材料拉伸模拟,应力应变曲线问题
  • ¥30 python代码,帮调试,帮帮忙吧
  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建