date_interval_format


php apg

FORMATS a date interval.



<?php

/* - - - - - - - - - - - - - - - - -

       Object oriented style 
   
   - - - - - - - - - - - - - - - - - */

str public DateInterval::format str $format )


where,

$format The format string
                          
SEE the below TABLE )
?>

<?php

/* - - - - - - - - - - - - - - - - -

      Procedural style
   
   - - - - - - - - - - - - - - - - - */

str date_interval_format obj DateInterval::format str $format )


where,

DateInterval Period designators

$format 
The format string
                          
SEE the below TABLE )

 
?>

 PERIOD DESIGNATORS 


The format starts with the letter P, for "period."
Each duration period is represented by an integer value followed by a period designator.

If the duration contains time elements, that portion of the specification is preceded by the letter T.

Period Designators
Period Designator Description
P Period
Y years
M months
D days
W weeks. These get converted into days,
so can not be combined with D.
T Time
H hours
M minutes
S seconds
ed48

The unit types must be entered from the largest scale unit on the left to the smallest scale unit on the right.

So years before months, months before days, days before minutes, etc.

Thus one year and four days must be represented as P1Y4D, not P4D1Y.

To know more about:

ISO 8601 Durations !


When the Period Designators cannot be parsed as an interval, the Exception is used.



 $format 

The following characters are recognized in the $format 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 -,
ed48


 RELATIVE FORMATS 

Used Symbols
Description Format
dayname 'sunday' | 'monday' | 'tuesday' | 'wednesday' | 'thursday' | 'friday' | 'saturday' |
| 'sun' | 'mon' | 'tue' | 'wed' | 'thu' | 'fri' | 'sat'
daytext 'weekday' | 'weekdays'
number [+-]?[0-9]+
ordinal 'first' | 'second' | 'third' | 'fourth' | 'fifth' | 'sixth' | 'seventh' |
| 'eighth' | 'ninth' | 'tenth' | 'eleventh' | 'twelfth' | 'next' | 'last' | 'previous' | 'this'
reltext 'next' | 'last' | 'previous' | 'this'
space [ \t]+
unit (('sec' | 'second' | 'min' | 'minute' | 'hour' | 'day' |
| 'fortnight' | 'forthnight' | 'month' | 'year') 's'?) | 'weeks' | daytext
ed48

Day-based Notations
Format Description Examples
'yesterday' Midnight of yesterday "yesterday 14:00"
'midnight' The time is set to 00:00:00  
'today' The time is set to 00:00:00  
'now' Now - this is simply ignored  
'noon' The time is set to 12:00:00 "yesterday noon"
'tomorrow' Midnight of tomorrow  
'back of' hour 15 minutes past the specified hour "back of 7pm", "back of 15"
'front of' hour 15 minutes before the specified hour "front of 5am", "front of 23"
'first day of' Sets the day of the first of the current month. This phrase is best used together with a month name following it. "first day of January 2008"
'last day of' Sets the day to the last day of the current month. This phrase is best used together with a month name following it. "last day of next month"
ordinal space dayname space 'of' Calculates the x-th week day of the current month. "first sat of July 2008"
'last' space dayname space 'of' Calculates the last week day of the current month. "last sat of July 2008"
number space? (unit | 'week') Handles relative time wtems where the value is a number. "+5 weeks", "12 day", "-7 weekdays"
ordinal space unit Handles relative time wtems where the value is text. "fifth day", "second month"
'ago' Negates all the values of previously found relative time wtems. "2 days ago", "8 days ago 14:00", "2 months 5 days ago", "2 months ago 5 days", "2 days ago"
dayname Moves to the next day of this name. "Monday"
reltext space 'week' Handles the special format "weekday + last/this/next week". "Monday next week"
ed48


  1 EXERCISE   

<?php

$obj01 
= new DateInterval('P1Y2M3DT4H5M6S');

foreach(
$obj01 as $k01 => $v01)
{
echo 
$k01 '&nbsp;=&nbsp;' $v01 '<br>';
}

?>

  2 EXERCISE   

<?php

$obj02 
= new DateInterval('P2Y4DT6H8M');
echo 
date_interval_format($obj02'%d days');

echo 
'<br>';

$obj02a = new DateInterval('P32D');
echo 
date_interval_format($obj02a'%d days');

?>

  3 EXERCISE   

<?php

$nw03a 
DateInterval::createFromDateString('2 years 3 weeks 8 hours 35 minutes 48 seconds');

echo 
$nw03a->format('%Y years %D days %H hours %I min %S sec');

echo 
'<br><br>The same result as:<br><br>';

$nw03b = new DateInterval('P2Y21DT8H35M48S');

echo 
$nw03b->format('%Y years %D days %H hours %I min %S sec');

echo 
'<br><br>';

?>

  4 EXERCISE   

<?php

$dt04aaa 
DateInterval::createFromDateString('tomorrow');
echo 
date_interval_format($dt04aaa'%d day(s)');

echo 
'<br><br>';

foreach(
$dt04aaa as $dt04aa => $dt04a)
{
echo 
$dt04aa '&nbsp;=&nbsp;' $dt04a '<br>';
}

echo 
'<br><br>The same as:<br><br>';

$dt04bbb = new DateInterval('P1D');

foreach(
$dt04bbb as $dt04bb => $dt04b)
{
echo 
$dt04bb '&nbsp;=&nbsp;' $dt04b '<br>';
}

?>

 RESULT   

1 day(s)

from_string = 1
date_string = tomorrow


The same as:

y = 0
m = 0
d = 1
h = 0
i = 0
s = 0
f = 0
invert = 0
days = 
from_string = 

  5 EXERCISE   

<?php

$dt05aaa 
DateInterval::createFromDateString('1 years 2 months 3 days 4 hours 5 minutes 6 seconds');
echo 
date_interval_format($dt05aaa'%Y-%M-%D %H:%I:%S');

echo 
'<br><br>The same as:<br><br>';

$dt05bbb = new DateInterval('P1Y2M3DT4H5M6S');
echo 
date_interval_format($dt05bbb'%Y-%M-%D %H:%I:%S');

?>

  6 EXERCISE   

<?php

if (!method_exists('DateInterval''format')) 
    die(
"skip: method doesn't exist");

date_default_timezone_set('UTC');

$date1 = new DateTime('2000-01-01 00:00:00');
$date2 = new DateTime('now');

$interval $date1->diff($date2);

echo 
$interval->format('Y=%Y') . "<br>";
echo 
$interval->format('M=%M') . "<br>";
echo 
$interval->format('D=%D') . "<br>";
echo 
$interval->format('H=%H') . "<br>";
echo 
$interval->format('I=%I') . "<br>";
echo 
$interval->format('S=%S') . "<br>";
echo 
$interval->format('R=%R') . "<br>";

echo 
$interval->format('y=%y') . "<br>";
echo 
$interval->format('m=%m') . "<br>";
echo 
$interval->format('d=%d') . "<br>";
echo 
$interval->format('h=%h') . "<br>";
echo 
$interval->format('i=%i') . "<br>";
echo 
$interval->format('s=%s') . "<br>";
echo 
$interval->format('r=%r') . "<br>";

echo 
"<br>";

$interval $date2->diff($date1);

echo 
$interval->format('inverted R=%R') . "<br>";
echo 
$interval->format('inverted r=%r') . "<br>";

echo 
"<br>";

echo 
$interval->format('%=%%') . "<br>";

// Invalid valid format character does not raise warnings.
echo $interval->format('x=%x') . "<br>";

?>

  7 EXERCISE   

<?php

try {
    new 
DateInterval('P3"D');
} catch (
Exception $e) {
    echo 
$e->getMessage(), "<br>";
}

try {
    new 
DatePeriod('P3"D');
} catch (
Exception $e) {
    echo 
$e->getMessage(), "<br>";
}

try {
    new 
DatePeriod('2008-03-01T12:00:00Z1');
} catch (
Exception $e) {
    echo 
$e->getMessage(), "<br>";
}

?>