Skip to content
This repository has been archived by the owner on Oct 31, 2023. It is now read-only.

Commit

Permalink
Manually instantiate first Friday Carbon instances to fix issues pert…
Browse files Browse the repository at this point in the history
…aining to Carbon::setTestNow() and timezones
  • Loading branch information
PHLAK committed Sep 2, 2017
1 parent 622f951 commit 5271380
Showing 1 changed file with 27 additions and 10 deletions.
37 changes: 27 additions & 10 deletions src/PHX2600/FirstFriday.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,7 @@ class FirstFriday
public function __construct($timezone, Carbon $now = null)
{
$this->timezone = $timezone;

if (isset($now)) {
Carbon::setTestNow($now);
}
$this->now = $now ?: Carbon::now($timezone);
}

/**
Expand All @@ -36,10 +33,20 @@ public function __construct($timezone, Carbon $now = null)
*/
public function next()
{
$firstFriday = new Carbon('first friday of this month', $this->timezone);
$firstFriday = new Carbon(sprintf(
'first friday of %s %s',
$this->now->format('F'),
$this->now->format('Y')
), $this->timezone);

if (Carbon::now()->gt($firstFriday)) {
return new Carbon('first friday of next month', $this->timezone);
if ($this->now->gt($firstFriday)) {
$this->now->addMonth();

return new Carbon(sprintf(
'first friday of %s %s',
$this->now->format('F'),
$this->now->format('Y')
), $this->timezone);
}

return $firstFriday;
Expand All @@ -52,10 +59,20 @@ public function next()
*/
public function previous()
{
$firstFriday = new Carbon('first friday of this month', $this->timezone);
$firstFriday = new Carbon(sprintf(
'first friday of %s %s',
$this->now->format('F'),
$this->now->format('Y')
), $this->timezone);

if ($this->now->lte($firstFriday)) {
$this->now->subMonth();

if (Carbon::now()->lte($firstFriday)) {
return new Carbon('first friday of last month', $this->timezone);
return new Carbon(sprintf(
'first friday of %s %s',
$this->now->format('F'),
$this->now->format('Y')
), $this->timezone);
}

return $firstFriday;
Expand Down

0 comments on commit 5271380

Please sign in to comment.