drza10046
drza10046
2014-04-09 12:28

如何使用mktime获取今天的时间

已采纳

I tried to get todays time at 00:01:00 (h:i:s)

my code:

<?php 
$delay_hour=10;
$delay_minute=11;

echo $t2=mktime($delay_hour, $delay_minute, 00, date('n',time()), date('j',time()), date('Y',time()));
echo '<br/>';
echo date("d-m-Y h:i:s",$t2);
echo '<br/>';
$delay_hour=0;
$delay_minute=1;

echo $t2=mktime($delay_hour, $delay_minute, 00, date('n',time()), date('j',time()), date('Y',time()));
echo '<br/>';
echo date("d-m-Y h:i:s",$t2);
echo '<br/>';
?>

so how to get todays time at 00:01:00?

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

2条回答

  • douba8048 douba8048 7年前

    Your code can be simplified to just:

    $result = mktime(0,1,0);//hour, minute, second
    

    Because mktime() without date part passed to it will set date to today's date.

    Note also, that mktime() results in timestamp - i.e. it's not formatted date, it's integer value (equal to number of seconds passed since Unix Epoch beginning)

    点赞 评论 复制链接分享
  • dourenzhou8740 dourenzhou8740 7年前
    var_dump(strtotime(date('Y-m-d').' 00:01:00'));
    

    or

    $date = new DateTime('today 00:01:00');
    var_dump($date->format('Y-m-d H:i:s')); // formatted
    var_dump($date->format('U')); // timestamp
    

    or

    var_dump(mktime(0, 1));
    

    If you need formatted representation, it is prefered to use second approach, with DateTime. If you need timestamp, use third, with mktime

    点赞 评论 复制链接分享