PHP is a very popular scripting language that is used by millions of websites and applications around the world. It has a rich set of features that allow developers to create complex and sophisticated applications. One area where PHP shines is in its support for dates and calendars. In this blog post, we will take a look at 3 of the best PHP date and calendar libraries.
Table of Contents
Carbon is a lightweight PHP library that makes working with dates and times much easier. It has an elegant, clean API and provides helpful methods for managing dates and times.
All of the functions from the base DateTime class are inherited by Carbon, so you can still access essential functionality like modify, format, and diff. But Carbon also adds a number of its own methods that make date and time management much simpler. Comparing dates with Carbon is a breeze too.
For example, the “copy” method creates an identical copy of a given DateTime object, while the “setDate” method sets the date portion of a Carbon object to a given value. Whether you’re looking to use the power of DateTime or simply want an easier way to work with dates and times, Carbon is a great option.
$mutable = Carbon::now(); $immutable = CarbonImmutable::now(); $modifiedMutable = $mutable->add(1, 'day'); $modifiedImmutable = CarbonImmutable::now()->add(1, 'day'); var_dump($modifiedMutable === $mutable); // bool(true) var_dump($mutable->isoFormat('dddd D')); // string(8) "Sunday 7" var_dump($modifiedMutable->isoFormat('dddd D')); // string(8) "Sunday 7" // So it means $mutable and $modifiedMutable are the same object // both set to now + 1 day. var_dump($modifiedImmutable === $immutable); // bool(false) var_dump($immutable->isoFormat('dddd D')); // string(10) "Saturday 6" var_dump($modifiedImmutable->isoFormat('dddd D')); // string(8) "Sunday 7" // While $immutable is still set to now and cannot be changed and // $modifiedImmutable is a new instance created from $immutable // set to now + 1 day. $mutable = CarbonImmutable::now()->toMutable(); var_dump($mutable->isMutable()); // bool(true) var_dump($mutable->isImmutable()); // bool(false) $immutable = Carbon::now()->toImmutable(); var_dump($immutable->isMutable()); // bool(false) var_dump($immutable->isImmutable()); // bool(true) //calculate different date time $dtToronto = Carbon::create(2012, 1, 1, 0, 0, 0, 'America/Toronto'); $dtVancouver = Carbon::create(2012, 1, 1, 0, 0, 0, 'America/Vancouver'); echo $dtVancouver->diffInHours($dtToronto); // 3
Aeon is a collection of libraries that make working with DateTime in PHP a breeze. The library is fully immutable, meaning that every operation on a DateTime object creates a new object – this helps to prevent bugs and side effects down the line.
Aeon is also focused on making the implicit, explicit – for example, by clearly identifying which timezone is being used. This makes it easy to work with timestamps from different sources and to ensure that your code is always running at the correct timezone.
use Aeon\Calendar\Gregorian\Day; use Aeon\Calendar\Gregorian\GregorianCalendar; use Aeon\Calendar\Gregorian\DateTime; use Aeon\Calendar\Gregorian\Month; use Aeon\Calendar\Gregorian\Time; use Aeon\Calendar\Gregorian\TimeZone; use Aeon\Calendar\Gregorian\Year; use Aeon\Calendar\TimeUnit; $dateTime = GregorianCalendar::UTC()->now(); $dateTime = DateTime::fromString('2020-01-01 00:00:00 UTC'); $dateTime = DateTime::fromDateTime( new DateTimeImmutable( '2020-01-01 00:00:00', new DateTimeZone('UTC') ) ); $dateTime = new DateTime( new Day(new Month(new Year(2020), 01), 01), new Time(00, 00, 00, 0), TimeZone::UTC() ); $dateTime = DateTime::create(2020, 01, 01, 00, 00, 00, 0, 'UTC'); //calendar $calendar = GregorianCalendar::UTC(); $calendar->currentYear() ->january() ->lastDay() ->noon() ->sub(TimeUnit::days(3));
Best of all, Aeon supports GPS, TAI, UTC, and UNIX timestamps – so you can be sure that your code will always be accurate, no matter where in the world it’s being used.
Brick\DateTime is a powerful set of classes to help you work with dates and times. It adds missing concepts such as LocalDate, LocalTime, YearMonth, MonthDay, and more, giving you a more comprehensive way to represent date and time information.
All of the classes are immutable, so you can safely pass them around without worrying about them being affected. Plus, they follow the ISO 8601 standard for representing date and time concepts, making them easy to work with and understand.
use Brick\DateTime\LocalDate; use Brick\DateTime\TimeZone; use Brick\DateTime\Clock\FixedClock; use Brick\DateTime\Instant; echo LocalDate::now(TimeZone::utc()); // 2022-10-18 $clock = new FixedClock(Instant::of(1000000000)); echo LocalDate::now(TimeZone::utc(), $clock); // 2001-09-09
These are just three of the best PHP date and calendar libraries available today. Each one has its own strength and weaknesses, so be sure to choose the one that is right for your needs. Thanks for reading!