Skip to content

Core\Time\DateTime::previous

$dateTime->previous(Core\Time\Weekday $w): Core\Time\DateTime$dateTime->previous(
    Core\Time\Weekday $w,
): Core\Time\DateTime

Returns a new date and time on the last earlier day that falls on a given weekday.

You give a case of Core\Time\Weekday, such as Core\Time\Weekday::Friday. The result is the nearest earlier day with that weekday. The time of day and the time zone stay the same, and the value you call previous on does not change.

previous always moves back, by one to seven days. When the value is already on that weekday, the result is seven days earlier.

Good to know: on a Monday, previous with Core\Time\Weekday::Monday gives the Monday of the week before. To find the Monday that a week started on, check the weekday first and keep a Monday as it is.

Parameters

  • $wCore\Time\WeekdayThe weekday to move to.

Return value

Returns Core\Time\DateTime. A new DateTime one to seven days earlier — a full week when the receiver already falls on $w; the receiver is unchanged.

Errors

  • RuntimeErrorThe result lies outside the representable range.

Changelog

VersionChange
0.0.5Core\Time\DateTime::weekday returns a Core\Time\Weekday case. Core\Time\DateTime::next and Core\Time\DateTime::previous take a Core\Time\Weekday case.

Examples

Find the last friday

<?nvs
// Shows how to find the last day before this one that falls on a given weekday.
// 11 June 2024 is a Tuesday.
var $tuesday = Core\Time\DateTime::fromIso('2024-06-11 09:15', 'Europe/Paris');
// `previous` returns a new value on the last Friday before it. The time of day stays the same.
// This prints Friday 7 June 2024, 09:15.
echo $tuesday->previous(Core\Time\Weekday::Friday)->format('EEEE d MMMM yyyy, HH:mm'), "\n";
// The last Monday is the day before. This prints Monday 10 June 2024, 09:15.
echo $tuesday->previous(Core\Time\Weekday::Monday)->format('EEEE d MMMM yyyy, HH:mm'), "\n";
Output
Friday 7 June 2024, 09:15
Monday 10 June 2024, 09:15

The same weekday is a week earlier

<?nvs
// Shows what `previous` returns when the value already falls on that weekday.
// 3 January 2025 is a Friday.
var $friday = Core\Time\DateTime::fromIso('2025-01-03 17:30', 'Asia/Tokyo');
// `previous` always moves back. On a Friday, the last Friday is seven days earlier.
// This prints 2024-12-27 17:30, which is in the year before.
echo $friday->previous(Core\Time\Weekday::Friday)->format('yyyy-MM-dd HH:mm'), "\n";
// The value itself does not change. This prints 2025-01-03 17:30.
echo $friday->format('yyyy-MM-dd HH:mm'), "\n";
Output
2024-12-27 17:30
2025-01-03 17:30

The monday a week started on

<?nvs
// Shows how to find the Monday that the current week started on, for a weekly timesheet.
var $zone = Core\Time\Zone::of('Europe/Madrid');
array<string> $entries = ['2024-05-15T10:00:00Z', '2024-05-19T10:00:00Z', '2024-05-20T10:00:00Z'];
// On a Monday, `previous` returns the Monday before, so a Monday is kept as it is.
// This prints Monday 13 May twice, then Monday 20 May.
foreach ($entries as string $text) {
var $day = Core\Time\Instant::fromIso($text)->in($zone);
if ($day->weekday() != Core\Time\Weekday::Monday) {
$day = $day->previous(Core\Time\Weekday::Monday);
}
echo 'Week of ', $day->format('EEEE d MMMM'), "\n";
}
Output
Week of Monday 13 May
Week of Monday 13 May
Week of Monday 20 May