问题描述
说我有DateInterval的这个对象实例:
$ obj = new DateInterval( P1Y12D);
现在我可以用 $ obj $ c做一些漂亮的事情$ c>实例,但是说我想从对象中删除
P1Y12D
字符串,是否有可能不需要重写类?
我没有找到一种方法,也许您找到了。
我的版本。。 p>
根据当前的PHP社区和PSR要求进行了改进。我也尝试使其更易读和直接。
/ **
* @param \ \DateInterval $ interval
*
* @返回字符串
* /
函数dateIntervalToString(\DateInterval $ interval){
//读取所有非-零日期部分。
$ date = array_filter(array(
'Y'=> $ interval-> y,
'M'=> $ interval-> m,
' D'=> $ interval-> d
));
//读取所有非零时间部分。
$ time = array_filter(array(
'H'=> $ interval-> h,
'M'=> $ interval-> i,
' S'=> $ interval-> s
));
$ specString ='P';
//将每个部分添加到规范字符串中。
foreach($ date as $ key => gt; $ value){
$ specString。= $ value。 $ key;
}
if(count($ time)> 0){
$ specString。=‘T’;
foreach($ time as $ key => $ value){
$ specString。= $ value。 $ key;
}
}
return $ specString;
}
这是扩展名最初的 \DateInterval
类:
类CustomDateInterval扩展了\DateInterval
{
公共函数__toString()
{
//读取所有非零日期部分。
$ date = array_filter(array(
'Y'=> $ this-> y,
'M'=> $ this-> m,
' D'=> $ this-> d
));
//读取所有非零时间部分。
$ time = array_filter(array(
'H'=> $ this-> h,
'M'=> $ this-> i,
' S'=> $ this-> s
));
$ specString ='P';
//将每个部分添加到规范字符串中。
foreach($ date as $ key => gt; $ value){
$ specString。= $ value。 $ key;
}
if(count($ time)> 0){
$ specString。=‘T’;
foreach($ time as $ key => $ value){
$ specString。= $ value。 $ key;
}
}
return $ specString;
}
}
可以这样使用:
$ interval = new CustomDateInterval('P1Y2M3DT4H5M6S');
//打印 P1Y2M3DT4H5M6S。
打印$ interval。 PHP_EOL;
我希望它会帮助某人,加油! / p>
Say I have this object instance of DateInterval:
$obj=new DateInterval("P1Y12D");
Now I can do few pretty things with that $obj
instance, but say I want to get out that "P1Y12D"
string from the object, is it straight possible without the need to override the class?
I do not find a method for this, maybe you do.
Here's my version of @Yaslaw's code.
It is improved according to a current PHP community and PSR requirements. I've also tried to make it more readable and straight-forward.
/**
* @param \DateInterval $interval
*
* @return string
*/
function dateIntervalToString(\DateInterval $interval) {
// Reading all non-zero date parts.
$date = array_filter(array(
'Y' => $interval->y,
'M' => $interval->m,
'D' => $interval->d
));
// Reading all non-zero time parts.
$time = array_filter(array(
'H' => $interval->h,
'M' => $interval->i,
'S' => $interval->s
));
$specString = 'P';
// Adding each part to the spec-string.
foreach ($date as $key => $value) {
$specString .= $value . $key;
}
if (count($time) > 0) {
$specString .= 'T';
foreach ($time as $key => $value) {
$specString .= $value . $key;
}
}
return $specString;
}
And here's the extension to the initial \DateInterval
class:
class CustomDateInterval extends \DateInterval
{
public function __toString()
{
// Reading all non-zero date parts.
$date = array_filter(array(
'Y' => $this->y,
'M' => $this->m,
'D' => $this->d
));
// Reading all non-zero time parts.
$time = array_filter(array(
'H' => $this->h,
'M' => $this->i,
'S' => $this->s
));
$specString = 'P';
// Adding each part to the spec-string.
foreach ($date as $key => $value) {
$specString .= $value . $key;
}
if (count($time) > 0) {
$specString .= 'T';
foreach ($time as $key => $value) {
$specString .= $value . $key;
}
}
return $specString;
}
}
It can be used like this:
$interval = new CustomDateInterval('P1Y2M3DT4H5M6S');
// Prints "P1Y2M3DT4H5M6S".
print $interval . PHP_EOL;
I hope it will help someone, cheers!
这篇关于是否有可能获得“ interval_spec”? DateInterval对象中的字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!