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条)

报告相同问题?

悬赏问题

  • ¥50 关于#html5#的问题:H5页面用户手机返回的时候跳转到指定页面例如(语言-javascript)
  • ¥15 无法使用此凭据登录,因为你的域不可用,如何解决?(标签-Windows)
  • ¥15 yolov9的训练时间
  • ¥15 二叉树遍历没有报错但无法正常运行
  • ¥15 在linux系统下vscode运行robocup3d上场球员报错
  • ¥15 Python语言实验
  • ¥15 SAP HANA SQL 增加合计行
  • ¥20 用C#语言解决一个英文打字练习器,有偿
  • ¥15 srs-sip外部服务 webrtc支持H265格式
  • ¥15 在使用abaqus软件中,继承到assembly里的surfaces怎么使用python批量调动