DateInterval::format

(PHP 5 >= 5.3.0, PHP 7, PHP 8)

DateInterval::formatFormats the interval

Description

public DateInterval::format(string $format): string

Formats the interval.

Parameters

format

The following characters are recognized in the format parameter string. Each format character must be prefixed by a percent sign (%).
format character Description Example values
% 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 Values

Returns the formatted interval.

Changelog

Version Description
7.1.0 The F and f format characters were added.

Examples

Example #1 DateInterval example

<?php

$interval 
= new DateInterval('P2Y4DT6H8M');
echo 
$interval->format('%d days');

?>

The above example will output:

4 days

Example #2 DateInterval and carry over points

<?php

$interval 
= new DateInterval('P32D');
echo 
$interval->format('%d days');

?>

The above example will output:

32 days

Example #3 DateInterval and DateTime::diff() with the %a and %d modifiers

<?php

$january 
= new DateTime('2010-01-01');
$february = new DateTime('2010-02-01');
$interval $february->diff($january);

// %a will output the total number of days.
echo $interval->format('%a total days')."\n";

// While %d will only output the number of days not already covered by the
// month.
echo $interval->format('%m month, %d days');

?>

The above example will output:

31 total days
1 month, 0 days

Notes

Note:

The DateInterval::format() method does not recalculate carry over points in time strings nor in date segments. This is expected because it is not possible to overflow values like "32 days" which could be interpreted as anything from "1 month and 4 days" to "1 month and 1 day".

See Also

Here you can write a comment


Please enter at least 10 characters.
Loading... Please wait.
* Pflichtangabe
There are no comments available yet.

Midjourney Tutorial - Instructions for beginners

There is an informative video about Midjourney, the tool for creating digital images using artificial intelligence, entitled "Midjourney tutorial in German - instructions for beginners" ...

Mike94

Autor : Mike94
Category: KI Tutorials

Basics of views in MySQL

Views in a MySQL database offer the option of creating a virtual table based on the result of an SQL query. This virtual table can be queried like a normal table without changing the underlying data. ...

admin

Autor : admin
Category: mySQL-Tutorials

Definition of stored procedures - an introduction

Stored procedures are predefined SQL code blocks that are stored in a database and can be called up as required. ...

Bernie

Autor : ebiz-consult GmbH & Co. KG
Category: mySQL-Tutorials

Publish a tutorial

Share your knowledge with other developers worldwide

Share your knowledge with other developers worldwide

You are a professional in your field and want to share your knowledge, then sign up now and share it with our PHP community

learn more

Publish a tutorial