Skip to content

Leaf 5 โ€” Utilities Reference โ€‹

Anchor (Security Guard) โ€‹

Anchor runs automatically in the background โ€” you rarely interact with it directly.

What it does automatically: โ€‹

  • XSS protection โ€” sanitizes all data coming through request(), session(), response(), etc.
  • SQL injection protection โ€” integrates with Leaf DB to auto-escape all query parameters
  • CSRF โ€” via the CSRF module (opt-in, see references/security.md)

Anchor's XSS and SQL protection only works with Leaf functions. If you use raw $_POST, $_GET, $_REQUEST, sanitize manually:

php
$data = anchor()->sanitize($_POST['data']);

Sanitization โ‰  validation. Always validate data too โ€” user input is evil and should never be trusted.


Dates โ€” tick() โ€‹

bash
leaf install date

Creating a Date โ€‹

php
tick();                              // now
tick('2018-01-01 12:00:00');         // from string
tick(new DateTime('2018-01-01'));     // from DateTime object

Getting & Setting Values โ€‹

php
// Getter (no arg) / Setter (with arg) โ€” returns new tick object
tick()->year();           tick()->year(2018);
tick()->month();          tick()->month(6);
tick()->day();            tick()->day(15);        // day of month
tick()->hour();           tick()->hour(12);
tick()->minute();         tick()->minute(30);
tick()->second();         tick()->second(0);
tick()->millisecond();    tick()->millisecond(0);

// Generic get/set
tick()->get('year');
tick()->set('year', 2018);

Units reference:

UnitShorthandDescription
dateDDay of month
daydDay of week (Sunday=0, Saturday=6)
monthMMonth (January=0, December=11)
yearyYear
hourhHour
minutemMinute
secondsSecond
millisecondmsMillisecond

Add & Subtract โ€‹

php
tick()->add(1, 'day');
tick()->add(1, 'week');
tick()->add(3, 'month');
tick()->subtract(1, 'day');
tick()->subtract(2, 'year');

Add/subtract units: day, week, month, year, hour, minute, second, millisecond (shorthand also accepted)

Start & End Of โ€‹

php
tick()->startOf('month');  // 2024-10-01 00:00:00
tick()->startOf('year');   // 2024-01-01 00:00:00
tick()->endOf('month');    // 2024-10-31 23:59:59
tick()->endOf('year');     // 2024-12-31 23:59:59

Available units: year, month, week, date/day, hour, minute, second

Chaining โ€‹

php
tick()
    ->startOf('month')
    ->add(1, 'day')
    ->set('year', 2018)
    ->format('YYYY-MM-DD HH:mm:ss');

Formatting โ€‹

php
tick()->format();                          // ISO8601: '2024-10-03T18:04:37+00:00'
tick('2019-01-25')->format('DD/MM/YYYY');  // '25/01/2019'

// Escape text in format string with brackets
tick('2019-01-25')->format('[Today is] DD/MM/YYYY');  // 'Today is 25/01/2019'

Format tokens:

TokenOutputDescription
YYYY20244-digit year
YY242-digit year
MMMMJanuaryFull month name
MMMJanAbbreviated month
MM01-12Month, 2-digit
M1-12Month
DD01-31Day of month, 2-digit
D1-31Day of month
ddddSundayFull day name
dddSunShort day name
ddSuMin day name
d0-6Day of week
HH00-23Hour, 24h, 2-digit
H0-23Hour, 24h
hh01-12Hour, 12h, 2-digit
h1-12Hour, 12h
mm00-59Minute, 2-digit
m0-59Minute
ss00-59Second, 2-digit
s0-59Second
SSS000-999Millisecond, 3-digit
AAM/PMAM/PM
aam/pmam/pm
Z+01:00UTC offset
ZZ+0100UTC offset (no colon)

Relative Time โ€‹

php
tick('2014-10-01')->fromNow();              // '10 years ago'
tick('2027-10-04')->fromNow();              // 'in 3 years'
tick('2014-10-01')->fromNow(true);          // '10 years' (no ago/in)

tick('2014-10-01')->from('2015-10-01');     // '1 year ago'
tick('2015-10-01')->from('2014-10-01');     // 'in 1 year'
tick('2014-10-01')->from('2015-10-01', true); // '1 year' (no ago/in)

Comparisons โ€‹

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

tick('2010-10-20')->isBetween('2010-10-19', new \DateTime('2010-10-25'));
tick('2010-10-20')->isBetweenOrEqual('2010-10-19', new \DateTime('2010-10-25'));
tick('2010-10-20')->isSameDay('2010-10-20');
tick('2010-10-20')->isSameMonth('2010-10-20');
tick('2010-10-20')->isSameYear('2010-10-20');
tick('2000-01-01')->isLeapYear();          // true
tick()->isDateTime('2000-01-01');          // false