I need to compare a date to today. Basically I put in 2/17/13 and it will output "yesterday". I've tried echo date('l jS F', strtotime('2/15/13')); but it will only display that date not compare it to today. 
            Asked
            
        
        
            Active
            
        
            Viewed 163 times
        
    -1
            
            
         
    
    
        vqdave
        
- 2,361
- 1
- 18
- 36
- 
                    possible duplicate of [Calculating relative time](http://stackoverflow.com/questions/11/calculating-relative-time) – jeremyharris Feb 18 '13 at 16:05
5 Answers
0
            
            
        From http://www.highlystructured.com/comparing_dates_php.html
$exp_date = "2006-01-16";
$todays_date = date("Y-m-d");
$today = strtotime($todays_date);
$expiration_date = strtotime($exp_date);
if ($expiration_date > $today) {
     $valid = "yes";
} else {
     $valid = "no";
}
 
    
    
        martriay
        
- 5,632
- 3
- 29
- 39
0
            
            
        Here's an example, to get you started:
$when = '2/17/13';
$now  = new DateTime();
// I append $now's time to $then, to make sure we compare 
// the two dates using the same time of the day
// if you want to compare to $then at 00:00:00, leave it out
$then = new DateTime( $when . ' ' . $now->format( 'H:i:s' ) );
$diff = $now->diff( $then );
switch( $diff->days )
{
    case 0:
        echo 'today';
    break;
    case 1:
        echo $diff->invert ? 'yesterday' : 'tomorrow';
    break;
    default:
        echo $diff->invert ? $diff->days . ' days ago' : $diff->days . ' days from now';
    break;
}
 
    
    
        Decent Dabbler
        
- 22,532
- 8
- 74
- 106
0
            
            
        Drupal ships with a very nice format_interval($interval, $granularity = 2, $langcode = NULL) function.
<?php
function format_interval($interval, $granularity = 2, $langcode = NULL) {
  $units = array(
    '1 year|@count years' => 31536000,
    '1 month|@count months' => 2592000,
    '1 week|@count weeks' => 604800,
    '1 day|@count days' => 86400,
    '1 hour|@count hours' => 3600,
    '1 min|@count min' => 60,
    '1 sec|@count sec' => 1,
  );
  $output = '';
  foreach ($units as $key => $value) {
    $key = explode('|', $key);
    if ($interval >= $value) {
      $output .= ($output ? ' ' : '') . format_plural(floor($interval / $value), $key[0], $key[1], array(), array('langcode' => $langcode));
      $interval %= $value;
      $granularity--;
    }
    if ($granularity == 0) {
      break;
    }
  }
  return $output ? $output : t('0 sec', array(), array('langcode' => $langcode));
}
?>
You don't need to be running Drupal to use it. Just include the above function somewhere. However, the above function also calls format_plural() - which calls t() (both custom Drupal functions), so you'll need to modify the above function or include all of them.
Your use case:
<?php
$today = date();
$compare = strtotime('2/15/13');
$interval = ($today - $compare);
print format_interval($interval, 1);
// Outputs '1 day'
?>
 
    
    
        Adam Balsam
        
- 785
- 9
- 15
0
            
            
        function checkDate($todaysDate,$expirationDate){
 $today = strtotime($todays_date);
 $expiration_date = strtotime($exp_date);
 if ($expiration_date > $today) {
     $valid = "yes";
 } else {
     $valid = "no";
 }
return $valid
}
$expirationDate = "2006-01-16";
$todaysDate = date("Y-m-d");
$valid=checkDate($todaysDate,$expirationDate)
Do it in a function then you can use it with any combination of dates. You can then expand upon the function with some conditionals to figure out if its "yesterday".
 
    
    
        cosmicsafari
        
- 3,949
- 11
- 37
- 56
0
            function relativeTime($time) {
$d[0] = array(1,"second");
$d[1] = array(60,"minute");
$d[2] = array(3600,"hour");
$d[3] = array(86400,"day");
$d[4] = array(604800,"week");
$d[5] = array(2592000,"month");
$d[6] = array(31104000,"year");
$w = array();
$return = "";
$now = time();
$diff = ($now-$time);
$secondsLeft = $diff;
for($i=6;$i>-1;$i--)
{
     $w[$i] = intval($secondsLeft/$d[$i][0]);
     $secondsLeft -= ($w[$i]*$d[$i][0]);
     if($w[$i]!=0)
     {
        $return.= abs($w[$i]) . " " . $d[$i][1] . (($w[$i]>1)?'s':'') ." ";
     }
}
$return .= ($diff>0)?"ago":"left";
return $return;
}
 
    
    
        vqdave
        
- 2,361
- 1
- 18
- 36