date_interval_format

Formats the interval

Syntax

date_interval_format ( DateInterval $object , string $format ) : string

Parameters

object

A DateInterval object.

format

Character Description Example
% Literal % %
Y Years, numeric, at least 2 digits with leading 0 01, 03
y Years, numeric 1, 3
M Months, numeric, at least 2 digits with leading 0 01, 03, 12
m Months, numeric 1, 3, 12
D Days, numeric, at least 2 digits with leading 0 01, 03, 31
d Days, numeric 1, 3, 31
a Total number of days as a result of a DateTime::diff() or (unknown) otherwise 4, 18, 8123
H Hours, numeric, at least 2 digits with leading 0 01, 03, 23
h Hours, numeric 1, 3, 23
I Minutes, numeric, at least 2 digits with leading 0 01, 03, 59
i Minutes, numeric 1, 3, 59
S Seconds, numeric, at least 2 digits with leading 0 01, 03, 57
s Seconds, numeric 1, 3, 57
F Microseconds, numeric, at least 6 digits with leading 0 007701, 052738, 428291
f Microseconds, numeric 7701, 52738, 428291
R Sign "-" when negative, "+" when positive -, +
r Sign "-" when negative, empty when positive -,

Return

Returns the formatted interval.

Examples

1

<?

$object = date_interval_create_from_date_string("1 year 1 month 1 day 1 hour 1 minute 1 second");
$format = "years: %y\nmonths: %m\ndays: %d\nhours: %h\nminutes: %i\nseconds: %s";

$return = date_interval_format($object, $format);

echo $return;

?>
years: 1
months: 1
days: 1
hours: 1
minutes: 1
seconds: 1

2

<?

$originObject = date_create("2001-01-01");
$targetObject = date_create();

$object = date_diff($originObject, $targetObject);
$format = "years: %y\nmonths: %m\ndays: %d\nhours: %h\nminutes: %i\nseconds: %s";

$return = date_interval_format($object, $format);

echo $return;

?>
years: 22
months: 1
days: 3
hours: 15
minutes: 57
seconds: 12

checkdate

date

date_add

date_create

date_create_from_format

date_create_immutable

date_create_immutable_from_format

date_date_set

date_default_timezone_get

date_default_timezone_set

date_diff

date_format

date_get_last_errors

date_interval_create_from_date_string

date_isodate_set

date_modify

date_offset_get

date_parse

date_parse_from_format

date_sub

date_sun_info

date_sunrise

date_sunset

date_time_set

date_timestamp_get

date_timestamp_set

date_timezone_get

date_timezone_set

getdate

gettimeofday

gmdate

gmmktime

gmstrftime

idate

localtime

microtime

mktime

strftime

strptime

strtotime

time

timezone_abbreviations_list

timezone_identifiers_list

timezone_location_get

timezone_name_from_abbr

timezone_name_get

timezone_offset_get

timezone_open

timezone_transitions_get

timezone_version_get

PHP

Home Menu