Time functions

"uweb/time.php"

Uray Web Library (UWeb)

Uray M. János © 2019-2024

This file contains several functions for dealing with time, e.g. converting it to user-friendly text.

Two languages are supported (by default): English and Hungarian.

Contents

Contents

1. Time constants

1.1. Current time

1.2. Time units

2. Convert to text

2.1. time_to_text()

2.2. time_to_relative_text()

2.3. time_output()

2.4. time_interval_text() and related

3. Miscellaneous

3.1. weekday_name()

1. Time constants

1.1. Current time
const now;

This constant contains the current time, expressed as the number of seconds since the Unix Epoch, 1 Jan 1970 00:00.

If the PHP script runs for a long time, current time means the time when time.php was included (for the first time).

1.2. Time units
const second = 1;
const minute = 60;
const hour   = 3600;
const day    = 86400;
const week   = 7*day;
const month  = 30*day;
const year   = 365*day;

These constants make expressing time more convenient for the functions in this file, which expect the time in seconds.

For example: time_to_text(now + 2*day - 5*hour).

Note: the units month and year are approximations.

2. Convert to text

2.1. time_to_text()
function time_to_text ($time, $params = null, $lang = null);

Convert the time to a textual representation (e.g. "19 Jun 1991 10:40").

$time: the time, expressed as the number of seconds since the Unix Epoch, 1 Jan 1970 00:00.

$params: parameters to control the exact format. It can be null or an array containing a subset of the following options:

$lang: language of the time format (e.g. "en" or "hu"). If omitted or null, the global lang constant is used.

2.2. time_to_relative_text()
function time_to_relative_text ($time, $lang = null);

Convert the time to a relative text, i.e. expressed as an approximate distance from the current time.

Examples: "6 days ago", "in 3 hours", "one year ago", "in two minutes", "now", "soon".

$time: the time, expressed as the number of seconds since the Unix Epoch, 1 Jan 1970 00:00.

$lang: language of the text (e.g. "en" or "hu"). If omitted or null, the global lang constant is used.

2.3. time_output()
function time_output ($time, $lang = null);

Convert the time to an HTML code, which shows it as a relative text like time_to_relative_text(), but when the user moves the mouse cursor over the text, a tooltip is displayed with the absolute time (like with time_to_text(), without $params).

The HTML code requires JavaScript with $has_tooltip.

$time: the time, expressed as the number of seconds since the Unix Epoch, 1 Jan 1970 00:00.

$lang: language of the text (e.g. "en" or "hu"). If omitted or null, the global lang constant is used.

2.4. time_interval_text() and related
function time_interval_text ($time, $lang = null);
function time_ago_text ($time, $lang = null);
function in_last_time_text ($time, $lang = null);
function in_time_text ($time, $lang = null);
function within_time_text ($time, $lang = null);
function for_time_text ($time, $lang = null);

Convert the time interval to an approximate textual representation.

$time: the time interval, either in seconds, or as a pair [number, unit], e.g. [3, "day"], where number is an integer or one of "few", "half" or "quarter", and unit is a string naming the unit, e.g. "hour" or "day", see Time units for the possible names.

$lang: language of the text (e.g. "en" or "hu"). If omitted or null, the global lang constant is used.

The different functions use different prepositions/suffices:

Function English example Hungarian example
time_interval_text() "3 days" "3 nap"
time_ago_text() "3 days ago" "3 napja"
in_last_time_text() "in the last 3 days" "az elmúlt 3 napban"
in_time_text() "in 3 days" "3 nap múlva"
within_time_text() "in 3 days" "3 napon belül"
for_time_text() "for 3 days" "3 napig"

3. Miscellaneous

3.1. weekday_name()
function weekday_name ($time, $lang = null);

Return the name of the weekday (e.g. "Monday").

$time: either the time, expressed as the number of seconds since the Unix Epoch, 1 Jan 1970 00:00, or a number between 0 and 7 for 0=Sunday, 1=Monday, …, 6=Saturday and 7=Sunday again.

$lang: language of the weekday (e.g. "en" or "hu"). If omitted or null, the global lang constant is used.