Skip to content

Commit

Permalink
Additional usage of DateTime in Time utility
Browse files Browse the repository at this point in the history
  • Loading branch information
bcrowe committed Nov 12, 2013
1 parent 145ab64 commit edd8eb6
Showing 1 changed file with 21 additions and 12 deletions.
33 changes: 21 additions & 12 deletions Cake/Utility/Time.php
Expand Up @@ -435,10 +435,12 @@ public static function niceShort($dateString = null, $timezone = null) {
* @link http://book.cakephp.org/2.0/en/core-libraries/helpers/time.html#TimeHelper::daysAsSql
*/
public static function daysAsSql($begin, $end, $fieldName, $timezone = null) {
$begin = static::fromString($begin, $timezone);
$end = static::fromString($end, $timezone);
$begin = date('Y-m-d', $begin) . ' 00:00:00';
$end = date('Y-m-d', $end) . ' 23:59:59';
$dateTime = new \DateTime;
$begin = $dateTime->setTimestamp(static::fromString($begin, $timezone))
->format('Y-m-d')
. ' 00:00:00';
$end = $dateTime->setTimestamp(static::fromString($end, $timezone))
->format('Y-m-d') . ' 23:59:59';

return "($fieldName >= '$begin') AND ($fieldName <= '$end')";
}
Expand Down Expand Up @@ -566,14 +568,16 @@ public static function isTomorrow($dateString, $timezone = null) {
* @link http://book.cakephp.org/2.0/en/core-libraries/helpers/time.html#TimeHelper::toQuarter
*/
public static function toQuarter($dateString, $range = false) {
$time = static::fromString($dateString);
$date = ceil(date('m', $time) / 3);
$dateTime = new \DateTime;
$timeStamp = $dateTime->setTimestamp(static::fromString($dateString));

$quater = ceil($timeStamp->format('m') / 3);
if ($range === false) {
return $date;
return $quater;
}

$year = date('Y', $time);
switch ($date) {
$year = $timeStamp->format('Y');
switch ($quater) {
case 1:
return array($year . '-01-01', $year . '-03-31');
case 2:
Expand Down Expand Up @@ -645,7 +649,9 @@ public static function toServer($dateString, $timezone = null, $format = 'Y-m-d
* @link http://book.cakephp.org/2.0/en/core-libraries/helpers/time.html#TimeHelper::toAtom
*/
public static function toAtom($dateString, $timezone = null) {
return date('Y-m-d\TH:i:s\Z', static::fromString($dateString, $timezone));
$dateTime = new \DateTime;
return $dateTime->setTimestamp(static::fromString($dateString, $timezone))
->format('Y-m-d\TH:i:s\Z');
}

/**
Expand All @@ -657,10 +663,12 @@ public static function toAtom($dateString, $timezone = null) {
* @link http://book.cakephp.org/2.0/en/core-libraries/helpers/time.html#TimeHelper::toRSS
*/
public static function toRSS($dateString, $timezone = null) {
$dateTime = new \DateTime;
$date = static::fromString($dateString, $timezone);

if ($timezone === null) {
return date("r", $date);
return $dateTime->setTimestamp($date)
->format('r');
}

$userOffset = $timezone;
Expand All @@ -679,7 +687,8 @@ public static function toRSS($dateString, $timezone = null) {
$minutes = (int)(fmod(abs($userOffset), $hours) * 60);
$timezone = ($userOffset < 0 ? '-' : '+') . str_pad($hours, 2, '0', STR_PAD_LEFT) . str_pad($minutes, 2, '0', STR_PAD_LEFT);
}
return date('D, d M Y H:i:s', $date) . ' ' . $timezone;
return $dateTime->setTimestamp($date)
->format('D, d M Y H:i:s') . ' ' . $timezone;
}

/**
Expand Down

0 comments on commit edd8eb6

Please sign in to comment.