2011-04-07 2 views
3

午後5時のように現在の時間をPHPで取得するには? 私は例えば午後5時のように現在の時間をPHPで取得するには?

<?php 
$current_time = ("h:i a"); 
if($current_time > "5:00 pm"){ 
    echo 'Hide'; 
}else{ 
    echo 'show'; 
} 
?> 

を決定を作成する必要があるため、私は、私は午後5時の後に「非表示」テキストを表示したい17:00 のようなPHPで時間を得ることに問題を抱えています。

誰でも知っていますか?

ありがとうございました。

非常に高く評価されています。

+0

ください尋ねる前に、検索機能を使用しています。ありがとうございました。 – Gordon

答えて

5

24時間対応システムを使用して、あなたはどんな問題を抱えています。if(date('G') > 17)

G        先頭にゼロを付けない時間の  24時間形式          0スルー23

0

具体的な例を使用して、人間の時間とコンピュータ時間のみを比較することはできません。これがstrtotimeが便利な理由です。

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'; 
} 
?> 
関連する問題