Time functions
"uweb/time.php"
Uray Web Library (UWeb)
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.
2.4. time_interval_text() and related
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).
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.
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:
"long"
, "short"
: make the format long or short, i.e. with or without the year. If neither option is given, it is decided based on how close this time is from the current time. "long-month"
: use the long form of the month, e.g. January, not Jan. "date"
: print only the date, not the time. "time"
: print only the time, not the date. "month"
: do not print the day, only the month and (possibly) the year (this option implies "date"
). "seconds"
: also print seconds, e.g. 12:34:56. $lang
: language of the time format (e.g. "en"
or "hu"
). If omitted or null, the global lang
constant is used.
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.
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.
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" |
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.