PHP calculate between two time with H:i:s format -
i'm trying find , calculate between startime
, finish time as: starttime + 1 hour
, current time. if current time between start , finish must print message such 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 not correct , can not fix that,
you can try this, shows 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 ); // minutes echo "try again in ".$tryagainin->format( "%i minutes" ); // or hours , minutes $hours = $tryagainin->format('%h'); $minutes = $tryagainin->format('%i'); echo "try again in $hours hours , $minutes minutes"; }
for more information take at: datetime::diff
Comments
Post a Comment