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.

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

报告相同问题?

悬赏问题

  • ¥120 计算机网络的新校区组网设计
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 海浪数据 南海地区海况数据,波浪数据
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?