PHPackages                             assoconnect/php-date - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. assoconnect/php-date

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

assoconnect/php-date
====================

PHP object to handle dates without time

v2.14.1(3mo ago)722.9k↓35.7%1[1 PRs](https://github.com/assoconnect/php-date/pulls)3MITPHPPHP ^8.3

Since Jan 22Pushed 3mo ago7 watchersCompare

[ Source](https://github.com/assoconnect/php-date)[ Packagist](https://packagist.org/packages/assoconnect/php-date)[ Docs](https://github.com/assoconnect/php-date/)[ RSS](/packages/assoconnect-php-date/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (10)Dependencies (2)Versions (32)Used By (3)

The PHP AbsoluteDate object
===========================

[](#the-php-absolutedate-object)

[![Build Status](https://github.com/assoconnect/php-date/actions/workflows/build.yml/badge.svg)](https://github.com/assoconnect/php-date/actions/workflows/build.yml)[![Quality Gate Status](https://camo.githubusercontent.com/077e1475c260378df1ba7670934911e009505a3c53f6414ec39f27e6f4e4efa2/68747470733a2f2f736f6e6172636c6f75642e696f2f6170692f70726f6a6563745f6261646765732f6d6561737572653f70726f6a6563743d6173736f636f6e6e6563745f7068702d64617465266d65747269633d616c6572745f737461747573)](https://sonarcloud.io/dashboard?id=assoconnect_php-date)

Why this object?
----------------

[](#why-this-object)

PHP has the `DateTime` class to handle date and time but there is no object to just handle a date.

A `DateTime` instance represents a precise point in time and embeds a `DateTimeZone` instance to format the underlying timestamp to a more readable format.

On the other hand, a date may have two meanings:

1. A period of 24h starting at 00:00:00 in the morning and ending at 23:59:59 in the evening. Actually a day may last less or more than 24 hours, for instance think about the day you advance your clock for the daylight saving time, but you get the idea: this date is actually an interval between two different `DateTime` instances. This interval changes when you travel the world: for instance a *date* starts about 8 hours later in Los Angeles than in Paris.
2. A simple reference to a day with no critical care about the timezone. Think about your birthday, you won't change it wherever you travel on Earth: you keep only the current one from the place you were born at.

This PHP `AbsoluteDate` object represents a date according to this second use case.

Why not use the `DateTime`, then?
---------------------------------

[](#why-not-use-the-datetime-then)

`DateTime` is an amazing class, and actually this `AbsoluteDate` class relies on it.

But you don't care about the time nor the timezone when you deal with this second use case.

Using `DateTime` may then lead to issues in case it is not handled properly.

It also helps to clearly identify if you are dealing with a real point in time or just a loose date.

How to use it?
--------------

[](#how-to-use-it)

This classes exposes two ways to instanciate a `AbsoluteDate` object:

1. Using the constructor, you get a `AbsoluteDate` instance using the default date format `Y-m-d`. If none is given, then the current date in the UTC timezone is used.
2. Using `AbsoluteDate::createInTimezone(\DateTimeZone $timezone, \DateTimeInterface $datetime = null)`, you get the current date of the given `DateTime` instance in the given timezone.

The `AbsoluteDate::format(string $format)` method can help you format the date as you want. It relies on the format method of the `DateTime` class thus it supports the same formats as the PHP date() function.

The `AbsoluteDate::startsAt(\DateTimeZone $timezone)` and `AbsoluteDate::endsAt(\DateTimeZone $timezone)` methods return a `DateTime` object in the given timezone. `startsAt` returns a `DateTime` at 00:00:00 whereas `endsAt` returns a `DateTime` at the end of the day (23:59:59).

Examples
--------

[](#examples)

### Using AbsoluteDate with DateTime

[](#using-absolutedate-with-datetime)

```
