Skip to content
Merged

Time #57856

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions src/Illuminate/Support/Carbon.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,42 @@ public static function createFromId(Uuid|Ulid|string $id): static

return static::createFromInterface($id->getDateTime());
}

/**
* Get the current date / time plus a given amount of time.
*/
public function plus(
int $years = 0,
int $months = 0,
int $weeks = 0,
int $days = 0,
int $hours = 0,
int $minutes = 0,
int $seconds = 0,
int $microseconds = 0
): static {
return $this->add("
$years years $months months $weeks weeks $days days
$hours hours $minutes minutes $seconds seconds $microseconds microseconds
");
}

/**
* Get the current date / time minus a given amount of time.
*/
public function minus(
int $years = 0,
int $months = 0,
int $weeks = 0,
int $days = 0,
int $hours = 0,
int $minutes = 0,
int $seconds = 0,
int $microseconds = 0
): static {
return $this->sub("
$years years $months months $weeks weeks $days days
$hours hours $minutes minutes $seconds seconds $microseconds microseconds
");
}
}
68 changes: 68 additions & 0 deletions src/Illuminate/Support/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@

namespace Illuminate\Support;

use Carbon\CarbonInterface;
use Carbon\CarbonInterval;
use Illuminate\Support\Defer\DeferredCallback;
use Illuminate\Support\Defer\DeferredCallbackCollection;
use Illuminate\Support\Facades\Date;
use Symfony\Component\Process\PhpExecutableFinder;

if (! function_exists('Illuminate\Support\defer')) {
Expand Down Expand Up @@ -47,3 +50,68 @@ function artisan_binary(): string
return defined('ARTISAN_BINARY') ? ARTISAN_BINARY : 'artisan';
}
}

// Time functions...

if (! function_exists('Illuminate\Support\now')) {
/**
* Create a new Carbon instance for the current time.
*
* @param \DateTimeZone|\UnitEnum|string|null $tz
* @return \Illuminate\Support\Carbon
*/
function now($tz = null): CarbonInterface
{
return Date::now(enum_value($tz));
}
}

if (! function_exists('Illuminate\Support\seconds')) {
/**
* Get the current date / time plus the given number of seconds.
*/
function seconds(int $seconds): CarbonInterval
{
return CarbonInterval::seconds($seconds);
}
}

if (! function_exists('Illuminate\Support\minutes')) {
/**
* Get the current date / time plus the given number of minutes.
*/
function minutes(int $minutes): CarbonInterval
{
return CarbonInterval::minutes($minutes);
}
}

if (! function_exists('Illuminate\Support\hours')) {
/**
* Get the current date / time plus the given number of hours.
*/
function hours(int $hours): CarbonInterval
{
return CarbonInterval::hours($hours);
}
}

if (! function_exists('Illuminate\Support\days')) {
/**
* Get the current date / time plus the given number of days.
*/
function days(int $days): CarbonInterval
{
return CarbonInterval::days($days);
}
}

if (! function_exists('Illuminate\Support\years')) {
/**
* Get the current date / time plus the given number of years.
*/
function years(int $years): CarbonInterval
{
return CarbonInterval::years($years);
}
}