dongwei1855
dongwei1855
2019-04-12 01:44
浏览 126

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
    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;
    
    点赞 评论
  • doucitao2944
    doucitao2944 2019-04-12 04:33

    You try like this:

    //your code
    echo strtotime($datafmt. ' 18:30:00'); //change comma (,) with dot (.)
    

    OR

    date_default_timezone_set('America/Argentina/Buenos_Aires');
    $data = new DateTime("18:30:00");
    $datafmt = $data->format('U');
    echo $datafmt; 
    
    点赞 评论

相关推荐