Skip to content
Open
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
20 changes: 20 additions & 0 deletions system/I18n/TimeTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -1028,6 +1028,26 @@ public function isAfter($testTime, ?string $timezone = null): bool
return $ourTimestamp > $testTimestamp;
}

/**
* Determines if the current instance's time is in the past.
*
* @throws Exception
*/
public function isPast(): bool
{
return $this->isBefore(static::now($this->timezone));
}

/**
* Determines if the current instance's time is in the future.
*
* @throws Exception
*/
public function isFuture(): bool
{
return $this->isAfter(static::now($this->timezone));
}

// --------------------------------------------------------------------
// Differences
// --------------------------------------------------------------------
Expand Down
28 changes: 28 additions & 0 deletions tests/system/I18n/TimeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1040,6 +1040,34 @@ public function testAfterWithMicroseconds(): void
$this->assertFalse($time2->isAfter($time1));
}

public function testIsPast(): void
{
Time::setTestNow('2025-12-30 12:00:00', 'Asia/Tehran');

$past = Time::parse('2025-12-30 11:59:59', 'Asia/Tehran');
$this->assertTrue($past->isPast());

$future = Time::parse('2025-12-30 12:00:01', 'Asia/Tehran');
$this->assertFalse($future->isPast());

$now = Time::now('Asia/Tehran');
$this->assertFalse($now->isPast());
}

public function testIsFuture(): void
{
Time::setTestNow('2025-12-30 12:00:00', 'Asia/Tehran');

$future = Time::parse('2025-12-30 12:00:01', 'Asia/Tehran');
$this->assertTrue($future->isFuture());

$past = Time::parse('2025-12-30 11:59:59', 'Asia/Tehran');
$this->assertFalse($past->isFuture());

$now = Time::now('Asia/Tehran');
$this->assertFalse($now->isFuture());
}

public function testHumanizeYearsSingle(): void
{
Time::setTestNow('March 10, 2017', 'America/Chicago');
Expand Down
1 change: 1 addition & 0 deletions user_guide_src/source/changelogs/v4.7.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,7 @@ Libraries
- **Image:** Added ``ImageMagickHandler::clearMetadata()`` method to remove image metadata for privacy protection.
- **ResponseTrait:** Added ``paginate``` method to simplify paginated API responses. See :ref:`ResponseTrait::paginate() <api_response_trait_paginate>` for details.
- **Time:** added methods ``Time::addCalendarMonths()`` and ``Time::subCalendarMonths()``
- **Time:** Added ``Time::isPast()`` and ``Time::isFuture()`` convenience methods. See :ref:`isPast <time-comparing-two-times-isPast>` and :ref:`isFuture <time-comparing-two-times-isFuture>` for details.

Commands
========
Expand Down
24 changes: 24 additions & 0 deletions user_guide_src/source/libraries/time.rst
Original file line number Diff line number Diff line change
Expand Up @@ -393,6 +393,30 @@ Works exactly the same as ``isBefore()`` except checks if the time is after the

.. literalinclude:: time/037.php

.. _time-comparing-two-times-isPast:

isPast()
--------

.. versionadded:: 4.7.0

Determines if the current instance's time is in the past, relative to "now".
It returns a boolean true/false::

.. literalinclude:: time/043.php

.. _time-comparing-two-times-isFuture:

isFuture()
----------

.. versionadded:: 4.7.0

Determines if the current instance's time is in the future, relative to "now".
It returns a boolean true/false::

.. literalinclude:: time/044.php

.. _time-viewing-differences:

Viewing Differences
Expand Down
6 changes: 6 additions & 0 deletions user_guide_src/source/libraries/time/043.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?php

use CodeIgniter\I18n\Time;

$time = Time::parse('yesterday');
echo $time->isPast(); // true
6 changes: 6 additions & 0 deletions user_guide_src/source/libraries/time/044.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?php

use CodeIgniter\I18n\Time;

$time = Time::parse('tomorrow');
echo $time->isFuture(); // true
Loading