Skip to content

Commit e4abd44

Browse files
committed
feat: add date documentation
1 parent 9d418e0 commit e4abd44

File tree

2 files changed

+347
-2
lines changed

2 files changed

+347
-2
lines changed

.vitepress/config/sidebar.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ const sidebar = [
8686
// { text: 'MVC Support', link: '/docs/database/mvc' },
8787
{ text: 'Query Builder', link: '/docs/database/builder' },
8888
{ text: 'Leaf Redis', link: '/docs/database/redis' },
89-
{ text: 'Other DB Engines', link: '/docs/database/other' },
89+
// { text: 'Other DB Engines', link: '/docs/database/other' },
9090
],
9191
},
9292
{

src/docs/utils/date.md

Lines changed: 346 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,346 @@
1-
# Leaf Date
1+
# Tick
2+
3+
Working with PHP dates can be challenging due to various date formats, handling different time zones, and tricky date calculations which are actually quite common. These issues can lead to inconsistencies if not managed carefully, and can be a source of bugs in your application.
4+
5+
Leaf provides a minimalistic module that provides a simple and clean API for working with dates in PHP. It is 100% compatible with PHP's native `DateTime` class, but offers a more fluent and expressive API inspired by Day.js.
6+
7+
```php
8+
tick()->now(); // get the current timestamp
9+
tick()->format('YYYY-MM-DD'); // format the current timestamp
10+
tick()
11+
->startOf('month')
12+
->add(1, 'day')
13+
->set('year', 2018)
14+
->format('YYYY-MM-DD HH:mm:ss');
15+
```
16+
17+
## Setting up
18+
19+
To get started with Tick, you need to install it using the Leaf CLI:
20+
21+
::: code-group
22+
23+
```bash:no-line-numbers [Leaf CLI]
24+
leaf install tick
25+
```
26+
27+
```bash:no-line-numbers [Composer]
28+
composer require leafs/tick
29+
```
30+
31+
:::
32+
33+
Once installed, you can start using Tick in your application.
34+
35+
## Parsing Dates
36+
37+
You probably noticed the `tick()` function in the examples above. This function is the entry point for Tick and initializes a date for manipulation or formatting. You can pass a date string, a timestamp, or a `DateTime` object to the `tick()` function.
38+
39+
```php
40+
tick(); // will use today's date
41+
tick('2018-01-01 12:00:00'); // create a date from a string
42+
43+
$date = new DateTime('2018-01-01 12:00:00');
44+
tick($date); // create a date from a DateTime object
45+
```
46+
47+
Tick is versatile and smart enough to handle dates correctly, so you can pass in any valid date string or timestamp and it will work as expected.
48+
49+
## Getting and Setting Dates
50+
51+
Tick provides methods for getting and setting various parts of a date, such as the year, month, day, hour, minute, second, and millisecond. This uses a syntax where the same function can be used to get or set a value.
52+
53+
```php
54+
tick()->second(30); // set the second to 30
55+
tick()->second(); // get the second
56+
```
57+
58+
This works for all parts of a date, including the year, month, day, hour, minute, second, and millisecond.
59+
60+
```php
61+
tick()->year(2018); // set the year to 2018
62+
tick()->year(); // get the year
63+
64+
tick()->month(); // gets current month
65+
tick()->month(0); // returns new tick object
66+
67+
tick()->day(); // gets day of current week
68+
tick()->day(0); // returns new tick object
69+
70+
tick()->date(); // gets day of current month
71+
tick()->date(1); // returns new tick object
72+
73+
tick()->hour(); // gets current hour
74+
newDate = tick()->hour(12); // returns new tick object
75+
76+
tick()->minute(); // gets current minute
77+
tick()->minute(59); // returns new tick object
78+
79+
tick()->second(); // gets current second
80+
tick()->second(1); // returns new tick object
81+
82+
tick()->millisecond(); // gets current millisecond
83+
tick()->millisecond(1); // returns new tick object
84+
```
85+
86+
You can also use the `get()` and `set()` methods to get and set values.
87+
88+
```php
89+
tick()->get('year'); // get the year
90+
tick()->set('year', 2018); // set the year to 2018
91+
```
92+
93+
### List of all available units
94+
95+
<!-- 3 column table populated with the data above -->
96+
| Unit | Shorthand | Description |
97+
| ----- | -------- | ----------- |
98+
| date | D | Date of Month |
99+
| day | d | Day of Week (Sunday as 0, Saturday as 6) |
100+
| month | M | Month (January as 0, December as 11) |
101+
| year | y | Year |
102+
| hour | h | Hour |
103+
| minute | m | Minute |
104+
| second | s | Second |
105+
| millisecond | ms | Millisecond |
106+
107+
## Manipulating Dates
108+
109+
This is a fancy way of saying "changing dates" into some other kind of date. Tick provides straightforward methods for adding, subtracting, setting, and manipulating dates in various ways.
110+
111+
*All manipulation methods return a new Tick object, so you can chain them together.*
112+
113+
```php
114+
tick()->add(1, 'day')->format('YYYY-MM-DD');
115+
```
116+
117+
## Adding and Subtracting Dates
118+
119+
Tick allows you to add time to a date using the `add()` method. You can add any number of years, months, days, hours, minutes, seconds, or milliseconds to a date.
120+
121+
```php
122+
tick()->add(1, 'day');
123+
tick()->add(1, 'week');
124+
```
125+
126+
You can also subtract time from a date using the `subtract()` method.
127+
128+
```php
129+
tick()->subtract(1, 'day');
130+
tick()->subtract(1, 'week');
131+
```
132+
133+
This is a list of all available units that can be used with the `add()` and `subtract()` methods:
134+
135+
| Unit | Shorthand | Description |
136+
| ------------- | --------- | ---------------------------------------- |
137+
| `day` | `d` | Day |
138+
| `week` | `w` | Week |
139+
| `month` | `M` | Month |
140+
| `year` | `y` | Year |
141+
| `hour` | `h` | Hour |
142+
| `minute` | `m` | Minute |
143+
| `second` | `s` | Second |
144+
| `millisecond` | `ms` | Millisecond |
145+
146+
## Getting the start/end of a unit
147+
148+
You can get the start of a unit using the `startOf()` method. This method sets the date to the start of the specified unit, such as the start of the day, month, or year. For example, to get the first day of the month or the first day of the year, you can use the following code:
149+
150+
```php
151+
tick()->startOf('month'); // => 2024-10-01 00:00:00
152+
tick()->startOf('year'); // => 2024-01-01 00:00:00
153+
```
154+
155+
You can also use the `endOf()` method to get the end of a unit. This method sets the date to the end of the specified unit, such as the end of the day, month, or year. For example, to get the last day of the month or the last day of the year, you can use the following code:
156+
157+
```php
158+
tick()->endOf('month'); // => 2024-10-31 23:59:59
159+
tick()->endOf('year'); // => 2024-12-31 23:59:59
160+
```
161+
162+
These methods are useful for getting the start or end of a unit when you need to perform calculations or comparisons based on the start or end of a unit.
163+
164+
The following units are available for use with the `startOf()` and `endOf()` methods:
165+
166+
| Unit | Shorthand | Description |
167+
| ------------- | --------- | ---------------------------------------- |
168+
| `year` | `y` | January 1st, 00:00 this year |
169+
| `month` | `M` | the first day of this month, 00:00 |
170+
| `week` | `w` | the first day of this week, 00:00 (locale aware) |
171+
| `date` | `D` | 00:00 today |
172+
| `day` | `d` | 00:00 today |
173+
| `hour` | `h` | now, but with 0 mins, 0 secs, and 0 ms |
174+
| `minute` | `m` | now, but with 0 seconds and 0 milliseconds |
175+
| `second` | `s` | now, but with 0 milliseconds |
176+
177+
## Formatting Dates
178+
179+
Formatting dates is a common task when working with dates in PHP. Formatting a date allows you to display the date in a specific format, such as `YYYY-MM-DD` or `DD/MM/YYYY`. Tick provides a `format()` method that allows you to format a date using a format string.
180+
181+
```php
182+
tick()->format();
183+
// '2024-10-03T18:04:37+00:00'
184+
// current date in ISO8601, without fraction seconds
185+
186+
tick('2019-01-25')->format('DD/MM/YYYY'); // '25/01/2019'
187+
```
188+
189+
You can combine various format tokens to create a custom date format. The following format tokens are available for use with the `format()` method:
190+
191+
| Format | Output | Description |
192+
| ------ | ------ | ---------------------------------------- |
193+
| `YY` | `70` | Two-digit year |
194+
| `YYYY` | `1970` | Four-digit year |
195+
| `M` | `1-12` | The month, beginning at 1 |
196+
| `MM` | `01-12` | The month, 2-digits |
197+
| `MMM` | `Jan-Dec` | The abbreviated month name |
198+
| `MMMM` | `January-December` | The full month name |
199+
| `D` | `1-31` | The day of the month |
200+
| `DD` | `01-31` | The day of the month, 2-digits |
201+
| `d` | `0-6` | The day of the week, with Sunday as 0 |
202+
| `dd` | `Su-Sa` | The min name of the day of the week |
203+
| `ddd` | `Sun-Sat` | The short name of the day of the week |
204+
| `dddd` | `Sunday-Saturday` | The name of the day of the week |
205+
| `H` | `0-23` | The hour |
206+
| `HH` | `00-23` | The hour, 2-digits |
207+
| `h` | `1-12` | The hour, 12-hour clock |
208+
| `hh` | `01-12` | The hour, 12-hour clock, 2-digits |
209+
| `m` | `0-59` | The minute |
210+
| `mm` | `00-59` | The minute, 2-digits |
211+
| `s` | `0-59` | The second |
212+
| `ss` | `00-59` | The second, 2-digits |
213+
| `SSS` | `000-999` | The millisecond, 3-digits |
214+
| `Z` | `+01:00` | Offset from UTC, e.g. +01:00 |
215+
| `ZZ` | `+0100` | Offset from UTC, e.g. +0100 |
216+
| `A` | `AM` | AM, PM |
217+
| `a` | `am` | am, pm |
218+
219+
Anything you pass into the `format()` method will be formatted according to the format string you provide. If you want to ignore parts of information you pass into the `format()` method, you can wrap them in square brackets.
220+
221+
```php
222+
tick('2019-01-25')->format('[YYYYescape] YYYY-MM-DDTHH:mm:ssZ[Z]');
223+
// 'YYYYescape 2019-01-25T00:00:000Z'
224+
```
225+
226+
YYYYescape got ignored instead of turning into a year. This is powerful, especially when you want to include some text in your date format.
227+
228+
## Time from now
229+
230+
You can get how far a date is from now using the `fromNow()` method. This method returns a string that represents the difference between the date and the current date in a human-readable format.
231+
232+
```php
233+
tick('2014-10-01')->fromNow(); // 10 years ago
234+
tick('2027-10-04')->fromNow(); // in 3 years
235+
```
236+
237+
If you want to remove the suffix (ago or in), you can pass `true` as the first argument to the `fromNow()` method.
238+
239+
```php
240+
tick('2014-10-01')->fromNow(true); // 10 years
241+
tick('2027-10-04')->fromNow(true); // 3 years
242+
```
243+
244+
## Time from a date
245+
246+
If you want to check how much time has passed since a specific date, you can use the `from()` method. This method returns a string that represents the difference between the date and the specified date in a human-readable format.
247+
248+
```php
249+
tick('2014-10-01')->from('2015-10-01'); // 1 year ago
250+
tick('2015-10-01')->from('2014-10-01'); // in 1 year
251+
```
252+
253+
If you want to remove the suffix (ago or in), you can pass `true` as the first argument to the `from()` method.
254+
255+
```php
256+
tick('2014-10-01')->from('2015-10-01', true); // 1 year
257+
tick('2015-10-01')->from('2014-10-01', true); // 1 year
258+
```
259+
260+
## Querying Dates
261+
262+
Querying dates allows you to check relationships between dates, such as whether a date is before or after another date. Tick provides methods for querying dates, such as `isBefore()`, `isAfter()`, and `isSame()`.
263+
264+
### Is Before
265+
266+
This indicates whether the Tick object is before the other supplied date-time.
267+
268+
```php
269+
tick()->isBefore('2011-01-01');
270+
```
271+
272+
### Is Same
273+
274+
This indicates whether the Tick object is the same as the other supplied date-time.
275+
276+
```php
277+
tick()->isSame(new \DateTime('2011-01-01'));
278+
```
279+
280+
### Is After
281+
282+
This indicates whether the Tick object is after the other supplied date-time.
283+
284+
```php
285+
tick()->isAfter('2011-01-01');
286+
```
287+
288+
### Is Between
289+
290+
This indicates whether the Tick object is between two other supplied date-time.
291+
292+
```php
293+
tick('2010-10-20')
294+
->isBetween('2010-10-19', new \DateTime('2010-10-25'));
295+
```
296+
297+
### Is Between Or Equal
298+
299+
This indicates whether the Tick object is between two other supplied date-time or equal to one of them.
300+
301+
```php
302+
tick('2010-10-20')
303+
->isBetweenOrEqual('2010-10-19', new \DateTime('2010-10-25'));
304+
```
305+
306+
### Is same day
307+
308+
This indicates whether the Tick object is the same day as the other supplied date-time.
309+
310+
```php
311+
tick('2010-10-20')->isSameDay('2010-10-20');
312+
```
313+
314+
### Is same month
315+
316+
This indicates whether the Tick object is the same month as the other supplied date-time.
317+
318+
```php
319+
320+
321+
tick('2010-10-20')->isSameMonth('2010-10-20');
322+
```
323+
324+
### Is same year
325+
326+
This indicates whether the Tick object is the same year as the other supplied date-time.
327+
328+
```php
329+
tick('2010-10-20')->isSameYear('2010-10-20');
330+
```
331+
332+
### Is Leap Year
333+
334+
This indicates whether the Tick object's year is a leap year or not.
335+
336+
```php
337+
tick('2000-01-01')->isLeapYear(); // true
338+
```
339+
340+
### Is DateTime
341+
342+
This indicates whether the Tick object is a DateTime object or not.
343+
344+
```php
345+
tick()->isDateTime('2000-01-01'); // false
346+
```

0 commit comments

Comments
 (0)