dongmin3754 2015-09-21 09:28
浏览 29
已采纳

PHP用H:i:s格式计算两次

i'm trying to find and calculate between startime, finish time as: starttime + 1 hour and current time. if current time is between start and finish i must be print message such as please try after 1 hour:

$current_date_time = new DateTime("now", new DateTimeZone("Asia/Tehran"));
$user_current_time = $current_date_time->format("H:i:s");

$start_limit_time = date("H:i:s",strtotime('2015-09-15 14:57:31'));
$finish_limit_time = date('H:i:s', strtotime($start_limit_time) + (60 * 60 * 1));

$date1 = DateTime::createFromFormat('H:i:s', $user_current_time);
$date2 = DateTime::createFromFormat('H:i:s', $start_limit_time);
$date3 = DateTime::createFromFormat('H:i:s', $finish_limit_time);
if ($date1 > $date2 && $date1 < $date3)
{
    echo 'here';
}

this code is not correct and i can not fix that,

  • 写回答

2条回答 默认 最新

  • doujianwan7570 2015-09-21 09:40
    关注

    You can try this, it shows the difference in minutes:

    $current_date_time = new DateTime("now", new DateTimeZone("Asia/Tehran"));
    $user_current_time = $current_date_time->format("H:i:s");
    
    $start_limit_time = date("H:i:s",strtotime('2015-09-15 14:57:31'));
    $finish_limit_time = date('H:i:s', strtotime($start_limit_time) + (60 * 60 * 1));
    
    $date1 = DateTime::createFromFormat('H:i:s', $user_current_time);
    $date2 = DateTime::createFromFormat('H:i:s', $start_limit_time);
    $date3 = DateTime::createFromFormat('H:i:s', $finish_limit_time);
    
    if ($date1 > $date2 && $date1 < $date3)
    {
        $tryAgainIn = $date3->diff( $date1 );
    
        // just minutes
        echo "try again in ".$tryAgainIn->format( "%i minutes" );
    
        // or hours and minutes
        $hours   = $tryAgainIn->format('%h');
        $minutes = $tryAgainIn->format('%i');
        echo "try again in $hours hours and $minutes minutes";
    }
    

    For more information take a look at: DateTime::diff

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥40 如果update 一个列名为参数的value
  • ¥15 基于51单片机的水位检测系统设计中LCD1602一直不显示
  • ¥15 OCS2安装出现问题,请大家给点意见
  • ¥15 ros小车启动launch文件报错
  • ¥15 vs2015到期想登陆但是登陆不上
  • ¥15 IPQ5018制作烧录固件,boot运行失败(操作系统-linux)(相关搜索:操作系统)(相关搜索:操作系统)
  • ¥20 icefall在librispeech基础上加入个人数据集
  • ¥30 keepalive高可用故障运维配置询问
  • ¥15 求帮助!国家电网内网u盘突然识别不出来了。
  • ¥15 matlab语音变速变调同时实现