note 113415 added to class.dateinterval
| From: | theDustin | Date: | Tue, 08 Oct 2013 15:35:03 +0000 |
| Subject: | note 113415 added to class.dateinterval | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-196497@lists.php.net to get a copy of this message | ||
If you want to format a \DateInterval to something like you input (new
\DateInterval("P3W2D")) you can use one of this:
<?php
class MyDateInterval extends \DateInterval
{
/**
* formating string like ISO 8601 (PnYnMnDTnHnMnS)
*/
const INTERVAL_ISO8601 = 'P%yY%mM%dDT%hH%iM%sS';
/**
* formating the interval like ISO 8601 (PnYnMnDTnHnMnS)
*
* @return string
*/
function __toString()
{
$sReturn = 'P';
if($this->y){
$sReturn .= $this->y . 'Y';
}
if($this->m){
$sReturn .= $this->m . 'M';
}
if($this->d){
$sReturn .= $this->d . 'D';
}
if($this->h || $this->i || $this->s){
$sReturn .= 'T';
if($this->h){
$sReturn .= $this->h . 'H';
}
if($this->i){
$sReturn .= $this->i . 'M';
}
if($this->s){
$sReturn .= $this->s . 'S';
}
}
return $sReturn;
}
}
?>
example use:
<?php
$oDateIntervalValue = new MyDateInterval('P3M');
$sFormatResult = $oDateIntervalValue->format(MyDateInterval::INTERVAL_ICALENDAR); //
"P0Y3M0DT0H0M0S"
$sToStringResult = (string) $oDateIntervalValue; // "P3M"
var_dump(new MyDateInterval($sFormatResult)); // object like $oDateIntervalValue
var_dump(new MyDateInterval($sToStringResult)); // object like $oDateIntervalValue
?>
----
Server IP: 213.172.110.14
Probable Submitter: 89.1.28.64
----
Manual Page -- http://php.net/manual/en/class.dateinterval.php
Edit -- https://master.php.net/note/edit/113415
Del: integrated -- https://master.php.net/note/delete/113415/integrated
Del: useless -- https://master.php.net/note/delete/113415/useless
Del: bad code -- https://master.php.net/note/delete/113415/bad+code
Del: spam -- https://master.php.net/note/delete/113415/spam
Del: non-english -- https://master.php.net/note/delete/113415/non-english
Del: in docs -- https://master.php.net/note/delete/113415/in+docs
Del: other reasons-- https://master.php.net/note/delete/113415
Reject -- https://master.php.net/note/reject/113415
Search -- https://master.php.net/manage/user-notes.php