datetime

HDateTime class with ISO-8601 parsing, formatting, arithmetic and relative time.

v1.0.0Math, time & IDsbundledimport "datetime";
Overview Installation Documentation Examples

Overview

Zero-dependency UTC date & time library for HolyPHP featuring the HDateTime class, ISO-8601 parsing and formatting, calendar arithmetic, and human-readable relative time.

bundled — works offline zero dependencies date time

Quick start

import "datetime";

$dt = dt_parse("2024-02-29T14:30:00Z");
println(dt_format($dt, "l, F j, Y g:i A"));
// Thursday, February 29, 2024 2:30 PM

$nextWeek = dt_add_days($dt, 7);
println($nextWeek->toIso8601());
// 2024-03-07T14:30:00Z

println(dt_relative($dt, $nextWeek));
// 7 days ago

Installation

In a project

Inside a project (a folder with hphp.yaml, see Projects & packages):

hphp install datetime

This adds the library to hphp.yaml, installs it into .hphp/packages/datetime/ and pins the exact version in hphp.lock:

require:
  datetime: "^1.0"

Use it

import "datetime";

datetime is bundled with the compiler, so import "datetime"; also works in a single file without installing anything, and hphp install works offline.

Outside a project

hphp install datetime          # for your user, shared by all scripts
hphp create myapp                 # or start a project, then: hphp install datetime
Packagedatetime
Version1.0.0 (follows the compiler)
Entry filedatetime.hphp
Sourcepackages/lib/datetime ↗

Documentation

API Reference

Class HDateTime

Represents an immutable UTC date and time.

Properties

Constructor

Methods

Functions

Error Handling

All validation errors throw an Exception prefixed with datetime::

Examples

01Quick start

Bundled with the compiler — import "datetime"; works offline in any file, or add it to a project with hphp install datetime.

import "datetime";

$dt = dt_parse("2024-02-29T14:30:00Z");
println(dt_format($dt, "l, F j, Y g:i A"));
// Thursday, February 29, 2024 2:30 PM

$nextWeek = dt_add_days($dt, 7);
println($nextWeek->toIso8601());
// 2024-03-07T14:30:00Z

println(dt_relative($dt, $nextWeek));
// 7 days ago
Try it
hphp install datetime
hphp run main.hphp

Covered by the official test suite in tests/positive/lib_datetime.hphp.