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 maixpy训练模型,模型训练好了以后,开发板通电会报错,不知道是什么问题
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 有没有帮写代码做实验仿真的
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog
  • ¥15 Excel发现不可读取的内容
  • ¥15 关于#stm32#的问题:CANOpen的PDO同步传输问题