Skip to content

Commit

Permalink
Fix datetime comparison in relative datetime functions.
Browse files Browse the repository at this point in the history
Closes #2987,#3514
  • Loading branch information
ADmad committed Apr 20, 2013
1 parent 842b180 commit ae8386c
Showing 1 changed file with 16 additions and 8 deletions.
24 changes: 16 additions & 8 deletions lib/Cake/Utility/CakeTime.php
Expand Up @@ -468,7 +468,8 @@ public static function dayAsSql($dateString, $fieldName, $timezone = null) {
*/
public static function isToday($dateString, $timezone = null) {
$timestamp = self::fromString($dateString, $timezone);
return date('Y-m-d', $timestamp) == date('Y-m-d', time());
$now = self::fromString('now', $timezone);
return date('Y-m-d', $timestamp) == date('Y-m-d', $now);
}

/**
Expand All @@ -481,7 +482,8 @@ public static function isToday($dateString, $timezone = null) {
*/
public static function isThisWeek($dateString, $timezone = null) {
$timestamp = self::fromString($dateString, $timezone);
return date('W o', $timestamp) == date('W o', time());
$now = self::fromString('now', $timezone);
return date('W o', $timestamp) == date('W o', $now);
}

/**
Expand All @@ -494,7 +496,8 @@ public static function isThisWeek($dateString, $timezone = null) {
*/
public static function isThisMonth($dateString, $timezone = null) {
$timestamp = self::fromString($dateString, $timezone);
return date('m Y', $timestamp) == date('m Y', time());
$now = self::fromString('now', $timezone);
return date('m Y', $timestamp) == date('m Y', $now);
}

/**
Expand All @@ -507,7 +510,8 @@ public static function isThisMonth($dateString, $timezone = null) {
*/
public static function isThisYear($dateString, $timezone = null) {
$timestamp = self::fromString($dateString, $timezone);
return date('Y', $timestamp) == date('Y', time());
$now = self::fromString('now', $timezone);
return date('Y', $timestamp) == date('Y', $now);
}

/**
Expand All @@ -521,7 +525,8 @@ public static function isThisYear($dateString, $timezone = null) {
*/
public static function wasYesterday($dateString, $timezone = null) {
$timestamp = self::fromString($dateString, $timezone);
return date('Y-m-d', $timestamp) == date('Y-m-d', strtotime('yesterday'));
$yesterday = self::fromString('yesterday', $timezone);
return date('Y-m-d', $timestamp) == date('Y-m-d', $yesterday);
}

/**
Expand All @@ -534,7 +539,8 @@ public static function wasYesterday($dateString, $timezone = null) {
*/
public static function isTomorrow($dateString, $timezone = null) {
$timestamp = self::fromString($dateString, $timezone);
return date('Y-m-d', $timestamp) == date('Y-m-d', strtotime('tomorrow'));
$tomorrow = self::fromString('tomorrow', $timezone);
return date('Y-m-d', $timestamp) == date('Y-m-d', $tomorrow);
}

/**
Expand Down Expand Up @@ -880,8 +886,9 @@ public static function wasWithinLast($timeInterval, $dateString, $timezone = nul

$date = self::fromString($dateString, $timezone);
$interval = self::fromString('-' . $timeInterval);
$now = self::fromString('now', $timezone);

return $date >= $interval && $date <= time();
return $date >= $interval && $date <= $now;
}

/**
Expand All @@ -902,8 +909,9 @@ public static function isWithinNext($timeInterval, $dateString, $timezone = null

$date = self::fromString($dateString, $timezone);
$interval = self::fromString('+' . $timeInterval);
$now = self::fromString('now', $timezone);

return $date <= $interval && $date >= time();
return $date <= $interval && $date >= $now;
}

/**
Expand Down

0 comments on commit ae8386c

Please sign in to comment.