是否可以从DateInterval对象中获取"interval_spec"字符串?

Tec*_*ist 5 php string dateinterval

假设我有DateInterval的这个对象实例:

$obj=new DateInterval("P1Y12D");
Run Code Online (Sandbox Code Playgroud)

现在我可以用这个$obj实例做一些漂亮的事情,但是说我想"P1Y12D"从对象中获取该字符串,是否可以直接实现而无需覆盖类?

我没有找到一种方法,也许你这样做.

Sla*_* II 9

这是我的@ Yaslaw 代码的版本.

它根据当前的PHP社区和PSR要求进行了改进.我也试图让它更具可读性和直接性.

/**
 * @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;
}
Run Code Online (Sandbox Code Playgroud)

这是初始\DateInterval课程的扩展:

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;
    }
}
Run Code Online (Sandbox Code Playgroud)

它可以像这样使用:

$interval = new CustomDateInterval('P1Y2M3DT4H5M6S');

// Prints "P1Y2M3DT4H5M6S".
print $interval . PHP_EOL;
Run Code Online (Sandbox Code Playgroud)

我希望它会帮助别人,欢呼!


Yas*_*law 5

您可以使用 format() 函数:

echo $obj->format('P%yY%mM%dDT%hH%iM%sS');
Run Code Online (Sandbox Code Playgroud)

或者更好的可读版本(字符串中没有零值):

function getSpecString(DateInterval $delta){
    //Read all date-parts there are not 0
    $date = array_filter(array('Y' => $delta->y, 'M' => $delta->m, 'D' => $delta->d));
    //Read all time-parts there are not 0
    $time = array_filter(array('H' => $delta->h, 'M' => $delta->i, 'S' => $delta->s));

    //Convert each part to spec-Strings
    foreach($date as $key => &$value) $value = $value.$key;
    foreach($time as $key => &$value) $value = $value.$key;

    //Create date spec-string
    $spec = 'P' . implode('', $date);            
    //add time spec-string
    if(count($time)>0) $spec .= 'T' . implode('', $time);
    return $spec;        
}   
Run Code Online (Sandbox Code Playgroud)