2015-06-24 38 views
0

在MySQL db中,我以「01:45:00 pm」格式存儲時間值。現在我必須檢查當前時間是否比數據庫存儲時間大。 我曾嘗試:php代碼來檢查greator比兩個給定的時間(例如,下午01:45:00和上午11:12:18)

<?php 
 

 
$count = 0; 
 

 
if('01:45:00 pm' >= '11:12:18 am') 
 
{ 
 
    $count++; //count is variable 
 
} 
 

 
echo $count; 
 

 
?>

它給結果爲0;所以請幫助我。

+2

如果(的strtotime('01:45:00')> =的strtotime('11 :12:18 am')) – splash58

+0

非常感謝。它適用於我... –

+0

好的。樂意效勞。 – splash58

回答

1

只需要使用的strtotime:

<?php 
$count=0; 
if(strtotime('01:45:00 pm') >= strtotime('11:12:18 am')) 
{ 
$count++;//count is variable 
} 
echo $count; 
?> 
2

使用對象而不是字符串爲您的日期時間

<?php 
$count = 0; 
$time_1 = new DateTimeImmutable('01:45:00 pm'); 
$time_2 = new DateTimeImmutable('11:12:18 am'); 
if ($time_1 >= $time_2) { 
    $count++; 
} 
echo $count; 
相關問題