dongwei1855 2019-04-12 01:44
浏览 162
已采纳

Php unix时间戳与strtotime

I start learn PHP and is very clear I make some error here, I try obtain Unix timestamp of specific hour and minutes of the day:

 <?php
 date_default_timezone_set('America/Argentina/Buenos_Aires');
 $data = new DateTime();
 $datafmt = $data->format('Y-m-d');
 echo strtotime($datafmt,'18:30:00');
 ?>

The code return 1554951600 and is equal to:

GMT: Thursday, April 11, 2019 3:00:00 AM
Your time zone: Thursday, April 11, 2019 12:00:00 AM GMT-03:00

This is wrong, timestamp should be:
1555018200 is equal to:

GMT: Thursday, April 11, 2019 9:30:00 PM
Your time zone: Thursday, April 11, 2019 6:30:00 PM GMT-03:00

What I doing wrong?


Fixed!

echo strtotime($datafmt. '18:30:00');   

, instead . that is my error!

  • 写回答

2条回答 默认 最新

  • drjyvoi734793 2019-04-15 19:20
    关注

    You do not need strtotime() at all. DateTime class is a replcement and is more powerful. Just pass the time to the constructor or set it with the method setTime()

    <?php
    date_default_timezone_set('America/Argentina/Buenos_Aires');
    $data = new DateTime('18:30:00');
    // Alternative ways to set the time of the DateTime object
    // $data->setTime('18', '30', '00');
    // $data->setTime(...explode(':', '18:30:00'));
    $datafmt = $data->format('U'); // U means UNIX timestamp
    echo $datafmt;
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 前置放大电路与功率放大电路相连放大倍数出现问题
  • ¥30 关于<main>标签页面跳转的问题
  • ¥80 部署运行web自动化项目
  • ¥15 腾讯云如何建立同一个项目中物模型之间的联系
  • ¥30 VMware 云桌面水印如何添加
  • ¥15 用ns3仿真出5G核心网网元
  • ¥15 matlab答疑 关于海上风电的爬坡事件检测
  • ¥88 python部署量化回测异常问题
  • ¥30 酬劳2w元求合作写文章
  • ¥15 在现有系统基础上增加功能