How to get Current time in php like 5:00 pm?
How to get Current time in php like 5:00 pm ? I have problem in getting time in php like 5:00 pm because I have to create a decision for example
<?php
$current_time = ("h:i a");
if($current_time > "5:00 pm"){
echo 'Hide';
}else{
echo 'show';
}
?>
I want to show the 'hide' text after 5:00 pm.
开发者_如何学编程anyone knows?
Thanks for the help.
Very Much appreciated.
Use the 24-hour-system and you won't have any problems: if(date('G') > 17)
G 24-hour format of an hour without leading zeros 0 through 23
Using your specific example, you cannot compare human time, only computer time. This is why strtotime is useful.
http://php.net/manual/en/function.strtotime.php
<?php
$current_time = time();
$my_time=strtotime( date('Y-m-d') . ' 5:00 pm' );
//uncomment if you want to check in the morning as well
//$my_earlytime=strtotime( date('Y-m-d') . ' 8:00 am' );
//uncomment this line if you want to check in the morning as well
//if ( $current_time > $my_time || $current_time < $my_earlytime ){
//comment this line if you want to check in the morning as well
if ( $current_time > $my_time ) {
//after 5:00pm it will hide
//hides before 8am if you check early time
echo 'Hide';
}
else {
//after midnight it will go back to show
//unless you also check the early time
echo 'show';
}
?>
精彩评论