2016-09-22 14 views
0

の機能で行うこのスクリプトを動作させる方法を調べようとしていますか?2つの日付間の日付を比較し、

基本的に、dbと今日の日付に格納された日付の差が10日より大きい場合、リンクが黒でなければリンクは赤で表示されます。日付がint(11)に格納されており、それがこの「1465929874」のようになり、データベース内の

<?php 
    // Today's date 
    $today   = date("d/m/Y"); 

    // The date stored into the DB 
    $NewsDate   = date("d/m/Y", strtotime($getInfo['date'])); 

    // The date stored into the DB + 10 days 
    $NewsDatePlus10  = date("d/m/Y", strtotime($NewsDate) + (86400 * 10)); 

    if ($NewsDate <= $NewsDatePlus10) { 
     echo "<span class='list-group-item-heading'><b>". utf8_encode($getInfo['title'])."</b><br /></span>"; 
     echo "<span class='list-group-item-text'>". utf8_encode($getInfo['content'])."&nbsp;&nbsp;<small>". date("d/m/Y", $getInfo['date'])."</small></span>";       
    } else { 
     echo "<span style='color:red;'>"; 
     echo "<span class='list-group-item-heading'><b>". utf8_encode($getInfo['title'])."</b><br /></span>"; 
     echo "<span class='list-group-item-text'>". utf8_encode($getInfo['content'])."&nbsp;&nbsp;<small>". date("d/m/Y", $getInfo['date'])."</small></span>"; 
     echo "</span>"; 
    } 
?> 

今あなたは私が最初の部分を設計していなかった想像できるように私はちょうどそれは私が必要なものをやるようにしようそれを行う。

+0

セパレータで区切られた日付は、strtotime()によってUS書式( 'm/d/Y')として扱われます。 –

答えて

0

私は、変数をチェックする以外の場所でその変数を使用していないときに、変数をd/m/Y形式に変換して変数に格納する理由がわかりません。

あなただけ行うことができる必要があります:

//Date stored in database 
$NewsDate   = strtotime($getInfo['date']); 

// The date stored into the DB + 10 days 
$NewsDatePlus10  = strtotime($NewsDate) + (86400 * 10); 

//Now you have a timestamp here. 
if ($NewsDate < $NewsDatePlus10) { 
     //Code here    
} else { 
0

は、テキストと数学をしないでください、あなたは車輪の再発明する必要はありません。

$today = new DateTime(); 
$NewsDate = new DateTimeImmutable($getInfo['date']); 
$NewsDatePlus10 = $NewsDate->modify('+10 days'); 
if ($NewsDate <= $NewsDatePlus10) { 
} else { 
} 

$getInfo['date']が入って来ていることを確認してください明白なフォーマット。

0

はここにあなたの完全な答えです:

<?php 
    $today = new DateTime(); 
    $NewsDate = date("d/m/Y", strtotime($getInfo['date'])); 
    $NewsDatePlus10 = $NewsDate->modify('+10 days'); 

      if ($NewsDate <= $NewsDatePlus10) { 
       echo "<span class='list-group-item-heading'><b>". utf8_encode($getInfo['title'])."</b><br /></span>"; 
       echo "<span class='list-group-item-text'>". utf8_encode($getInfo['content'])."&nbsp;&nbsp;<small>". date("d/m/Y", $getInfo['date'])."</small></span>";       
      } else { 
       echo "<span style='color:red;'>"; 
       echo "<span class='list-group-item-heading'><b>". utf8_encode($getInfo['title'])."</b><br /></span>"; 
       echo "<span class='list-group-item-text'>". utf8_encode($getInfo['content'])."&nbsp;&nbsp;<small>". date("d/m/Y", $getInfo['date'])."</small></span>"; 
       echo "</span>"; 
      } 
     ?> 

しかし、あなたが取得しているときに、日付形式を確認してください。

日付を追加する場合は、 $NewDate=Date('Y-m-d', strtotime("+3 days"));

$NewDate=Date('Y-m-d', strtotime("-3 days"));

0

この例は、必要に応じて使用できます。これは、あなたの年齢の日をあなたに与えます。

$now = time(); // current time 
$your_date = strtotime("15-09-2016"); // db date 
$datediff = $now - $your_date; 
$no_of_days = floor($datediff/(60 * 60 * 24)); 

if($no_of_days > 10){ echo 'red';} 
else { echo 'black';}