Skip to content

Leaf Date

Leaf Date (now known as tick) is a minimalist PHP library that parses, validates, manipulates, and displays dates and times with a largely DayJS/MomentJS-compatible API. If you use DayJS, you already know how to use Tick.

tick()->now(); // get the current timestamp
tick()->format('YYYY-MM-DD'); // format the current timestamp
tick()->startOf('month')->add(1, 'day')->set('year', 2018)->format('YYYY-MM-DD HH:mm:ss');

Why Tick?

3kB

Less PHP to download, parse and execute, leaving more time for your code.

Simple

Tick is a minimalist PHP library that parses, validates, manipulates, and displays dates and times a largely Day JS and Moment.js compatible API. If you use Day JS or Moment.js, you already know how to use Tick.

Installation

You can install Tick through Leaf CLI:

leaf install date

Or with composer:

composer require leafs/date

Date Operations

The documentation will cover methods divided into 4 categories:

Parsing

Instead of modifying the native DateTime object, Tick creates a wrapper for the Date object. To get this wrapper object, simply call tick() with one of the supported input types.

tick('2018-01-01 12:00:00'); // create a date from a string
tick(); // will use today's date

The tick() function

Calling tick() without parameters returns a fresh Tick object. You can then use any of the tick methods to parse, manipulate, or display the date.

$now = tick()

This is essentially the same as calling tick('with today\'s date').

String

Parse the given string in ISO 8601 format (a space instead of the 'T' is allowed) and return a Tick object instance.

tick('2018-04-04T16:00:00.000Z')
tick('2018-04-13 19:18:17.040+02:00')
tick('2018-04-13 19:18')

Date

Create a Tick object with a pre-existing native DateTime object.

$d = new DateTime();
tick(d);

Get + Set

Tick uses overloaded getters and setters, that is to say, calling these methods without parameters acts as a getter, and calling them with a parameter acts as a setter.

These map to the corresponding function on the native DateTime object.

tick()->second(30); // set the second to 30
tick()->second(); // get the second

Millisecond

Gets or sets the millisecond.

Accepts numbers from 0 to 999. If the range is exceeded, it will bubble up to the seconds.

tick()->millisecond(); // gets current millisecond
tick()->millisecond(1); // returns new tick o->ject

Second

Gets or sets the second.

Accepts numbers from 0 to 59. If the range is exceeded, it will bubble up to the minutes.

tick()->second(); // gets current second
tick()->second(1); // returns new tick object

Minute

Gets or sets the minutes.

Accepts numbers from 0 to 59. If the range is exceeded, it will bubble up to the hour.

tick()->minute(); // gets current minute
tick()->minute(59); // returns new tick object

Hour

Gets or sets the hour.

Accepts numbers from 0 to 23. If the range is exceeded, it will bubble up to the day.

tick()->hour(); // gets current hour
newDate = tick()->hour(12); // returns new tick object

Date of Month

Gets or sets the day of the month.

Accepts numbers from 1 to 31. If the range is exceeded, it will bubble up to the months.

tick()->date(); // gets day of current month
tick()->date(1); // returns new tick object

Day of Week

Gets or sets the day of the week.

Accepts numbers from 0 (Sunday) to 6 (Saturday). If the range is exceeded, it will bubble up to other weeks.

tick()->day(); // gets day of current week
tick()->day(0); // returns new tick object

Month

Gets or sets the month.

Accepts numbers from 0 to 11. If the range is exceeded, it will bubble up to the year.

tick()->month(); // gets current month
tick()->month(0); // returns new tick object

Year

Gets or sets the year.

tick()->year(); // gets current year
tick()->year(2000); // returns new tick object

Get

String getter, returns the corresponding information getting from Tick object. Units are case insensitive, short forms are case sensitive.

tick()->get('year');
tick()->get('month'); // start 0
tick()->get('date');
tick()->get('hour');
tick()->get('minute');
tick()->get('second');
tick()->get('millisecond');

List of all available units

UnitShorthandDescription
dateDDate of Month
daydDay of Week (Sunday as 0, Saturday as 6)
monthMMonth (January as 0, December as 11)
yearyYear
hourhHour
minutemMinute
secondsSecond
millisecondmsMillisecond

Set

Generic setter, accepting unit as first argument, and value as second, returns a new instance with the applied changes.

In general:

tick()->set('date', 1);
tick()->set('month', 3); // April
tick()->set('second', 30);

For multiple set:

tick()->set('hour', 5)->set('minute', 55)->set('second', 15);

Manipulation

Once you have a Tick object, you may want to manipulate it in some way.

Tick supports method chaining like this:

tick('2019-01-25')->add(1, 'day')->subtract(1, 'year')->year(2009)->toString()

Add

Returns a cloned Tick object with a specified amount of time added.

$a = tick();
$b = a->add(7, 'day')

// $a -> the original value and will not change
// $b -> the manipulation result

Units are case insensitive, short forms are case sensitive.

List of all available add units

UnitShorthandDescription
daydDay
weekwWeek
monthMMonth
yearyYear
hourhHour
minutemMinute
secondsSecond
millisecondmsMillisecond

Subtract

Returns a cloned Tick object with a specified amount of time subtracted.

tick()->subtract(7, 'year');

Units are case insensitive, and support plural and short forms.

List of all available units.

Start of Time

Returns a cloned Tick object and set it to the start of a unit of time.

tick()->startOf('year')

Units are case insensitive, and support plural and short forms.

List of all available start units

UnitShorthandDescription
yearyJanuary 1st, 00:00 this year
monthMthe first day of this month, 00:00
weekwthe first day of this week, 00:00 (locale aware)
dateD00:00 today
dayd00:00 today
hourhnow, but with 0 mins, 0 secs, and 0 ms
minutemnow, but with 0 seconds and 0 milliseconds
secondsnow, but with 0 milliseconds

End of Time

Returns a cloned Tick object and set it to the end of a unit of time.

tick()->endOf('month');

The list of all available units is the same as startOf.

Display

Once parsing and manipulation are done, you need some way to display the Tick object.

Format

Get the formatted date according to the string of tokens passed in.

To escape characters, wrap them in square brackets (e.g. [MM]).

tick()->format();
// '2023-04-02T18:04:37+00:00'
// current date in ISO8601, without fraction seconds

tick('2019-01-25')->format('[YYYYescape] YYYY-MM-DDTHH:mm:ssZ[Z]');
// 'YYYYescape 2019-01-25T00:00:000Z'

tick('2019-01-25')->format('DD/MM/YYYY'); // '25/01/2019'

List of all available formats

FormatOutputDescription
YY70Two-digit year
YYYY1970Four-digit year
M1-12The month, beginning at 1
MM01-12The month, 2-digits
MMMJan-DecThe abbreviated month name
MMMMJanuary-DecemberThe full month name
D1-31The day of the month
DD01-31The day of the month, 2-digits
d0-6The day of the week, with Sunday as 0
ddSu-SaThe min name of the day of the week
dddSun-SatThe short name of the day of the week
ddddSunday-SaturdayThe name of the day of the week
H0-23The hour
HH00-23The hour, 2-digits
h1-12The hour, 12-hour clock
hh01-12The hour, 12-hour clock, 2-digits
m0-59The minute
mm00-59The minute, 2-digits
s0-59The second
ss00-59The second, 2-digits
SSS000-999The millisecond, 3-digits
Z+01:00Offset from UTC, e.g. +01:00
ZZ+0100Offset from UTC, e.g. +0100
AAMAM, PM
aamam, pm

Time from Now

Returns the string of relative time from now.

@>RelativeTime


tick('2013-01-01')->fromNow(); // 10 years ago

If you pass true, you can get the value without the prefix/suffix.


tick('2013-01-01')->fromNow(true); // 10 years

Time from X

Returns the string of relative time from X.

tick('1999-01-01')->from('2000-01-01'); // a year ago

If you pass true, you can get the value without the suffix.

tick('1999-01-01')->from('2000-01-01', true); // a year

Time to Now

Returns the string of relative time to now.


tick('2033-01-01')->toNow(); // in 10 years

If you pass true, you can get the value without the prefix/suffix.


tick('2033-01-01')->toNow(true); // 10 years

Querying

There are several methods to query a Tick object.

Is Before

This indicates whether the Tick object is before the other supplied date-time.

tick()->isBefore('2011-01-01');

Is Same

This indicates whether the Tick object is the same as the other supplied date-time.

tick()->isSame(new \DateTime('2011-01-01'));

Is After

This indicates whether the Tick object is after the other supplied date-time.

tick()->isAfter('2011-01-01');

Is Between

This indicates whether the Tick object is between two other supplied date-time.


tick('2010-10-20')->isBetween('2010-10-19', new \DateTime('2010-10-25'));

Is Between Or Equal

This indicates whether the Tick object is between two other supplied date-time or equal to one of them.


tick('2010-10-20')->isBetweenOrEqual('2010-10-19', new \DateTime('2010-10-25'));

Is same day

This indicates whether the Tick object is the same day as the other supplied date-time.

tick('2010-10-20')->isSameDay('2010-10-20');

Is same month

This indicates whether the Tick object is the same month as the other supplied date-time.



tick('2010-10-20')->isSameMonth('2010-10-20');

Is same year

This indicates whether the Tick object is the same year as the other supplied date-time.

tick('2010-10-20')->isSameYear('2010-10-20');

Is Leap Year

This indicates whether the Tick object's year is a leap year or not.

tick('2000-01-01')->isLeapYear(); // true

Is DateTime

This indicates whether the Tick object is a DateTime object or not.

tick()->isDateTime('2000-01-01'); // false
Leaf Date has loaded