Utility class for working with time and date, providing constants, date calculations, formatting, and helpers for months, quarters, and years.
Provides predefined time-related constants in seconds for convenience, including seconds, minutes, days, weeks, months, and years.
<?php
use Krystal\Date\TimeHelper;
echo TimeHelper::SECOND;
echo TimeHelper::MINUTE;
echo TimeHelper::DAY;
echo TimeHelper::WEEK;
echo TimeHelper::MONTH;
echo TimeHelper::YEAR;
Returns a copyright year or range from start year to current year.
echo TimeHelper::getCopyright(2020); // 2020 - 2025 (if current year is 2025)
Check if timestamp is valid
var_dump(TimeHelper::isTimestamp(1609459200)); // true
var_dump(TimeHelper::isTimestamp("not a timestamp")); // false
Formats a date in a localized pattern using ICU formatting.
echo TimeHelper::formatLocalized("2025-10-04", "en_US", "MMMM dd, yyyy");
// October 04, 2025
Guesses the current season based on the Northern Hemisphere calendar.
echo TimeHelper::guessSeason(); // Spring / Summer / Fall / Winter
Checks whether a datetime string is valid.
var_dump(TimeHelper::formatValid("2025-10-04")); // true
var_dump(TimeHelper::formatValid("invalid-date")); // false
Calculates age from a given birth date.
echo TimeHelper::age("1990-05-15"); // 35 (if current year is 2025)
Returns all days of the month as an array.
print_r(TimeHelper::getDays());
Output
Array
(
[1] => 1
[2] => 2
...
[31] => 31
)
Returns the current date, optionally with time.
echo TimeHelper::getNow(); // 2025-10-04 12:34:56
echo TimeHelper::getNow(false); // 2025-10-04
Checks whether the end date has passed compared to a start date.
var_dump(TimeHelper::isExpired("2025-10-01", "2025-10-04")); // false
var_dump(TimeHelper::isExpired("2025-10-05", "2025-10-04")); // true
Calculates time difference between two timestamps.
$start = strtotime("2025-10-04 08:00:00");
$end = strtotime("2025-10-04 12:30:45");
echo TimeHelper::getTakenTime($start, $end); // 04:30:45
Returns number of days in a specific month and year.
echo TimeHelper::getMonthDaysCount(2, 2024); // 29 (leap year)
Returns months preceding the target month.
print_r(TimeHelper::getPreviousMonths("05"));
// ['01', '02', '03', '04', '05']
Returns months following the target month.
print_r(TimeHelper::getNextMonths("05"));
// ['05', '06', '07', '08', '09', '10', '11', '12']
Returns all months with names.
print_r(TimeHelper::getMonths());
Output
Array
(
['01'] => January
['02'] => February
...
['12'] => December
)
Returns all quarters in a year.
print_r(TimeHelper::getQuarters()); // [1, 2, 3, 4]
Returns months from Q1 up to a given quarter.
print_r(TimeHelper::getAllMonthsByQuarter(2));
// ['01', '02', '03', '04', '05', '06']
Returns months of a specific quarter.
print_r(TimeHelper::getMonthsByQuarter(3)); // ['07','08','09']
echo TimeHelper::getQuarter(5); // 2
echo TimeHelper::getQuarter(); // current quarter
Generates an array of years from start to end.
print_r(TimeHelper::createYears(2020, 2025));
Output
Array
(
[2020] => 2020
[2021] => 2021
...
[2025] => 2025
)
Generates an array of years from start up to the current year.
print_r(TimeHelper::createYearsUpToCurrent(2018));
Output
Array
(
[2018] => 2018
[2019] => 2019
...
[2025] => 2025
)