說我有DateInterval這個對象實例:是否有可能從DateInterval對象中獲取「interval_spec」字符串?
$obj=new DateInterval("P1Y12D");
現在我可以做一些漂亮的東西與$obj
實例,但說我想指出,從對象"P1Y12D"
字符串,是直接可以沒有需要重寫類?
我沒有找到一個方法,也許你這樣做。
說我有DateInterval這個對象實例:是否有可能從DateInterval對象中獲取「interval_spec」字符串?
$obj=new DateInterval("P1Y12D");
現在我可以做一些漂亮的東西與$obj
實例,但說我想指出,從對象"P1Y12D"
字符串,是直接可以沒有需要重寫類?
我沒有找到一個方法,也許你這樣做。
根據當前的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;
}
而這裏的擴展初始\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;
}
}
它可以像這樣使用:
$interval = new CustomDateInterval('P1Y2M3DT4H5M6S');
// Prints "P1Y2M3DT4H5M6S".
print $interval . PHP_EOL;
我希望這會幫助別人,歡呼!
我不是一個C大師,但在構造函數的源代碼的價值似乎並沒有在所有存儲:
/* {{{ proto DateInterval::__construct([string interval_spec])
Creates new DateInterval object.
*/
PHP_METHOD(DateInterval, __construct)
{
char *interval_string = NULL;
int interval_string_length;
php_interval_obj *diobj;
timelib_rel_time *reltime;
zend_error_handling error_handling;
zend_replace_error_handling(EH_THROW, NULL, &error_handling TSRMLS_CC);
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &interval_string, &interval_string_length) == SUCCESS) {
if (date_interval_initialize(&reltime, interval_string, interval_string_length TSRMLS_CC) == SUCCESS) {
diobj = zend_object_store_get_object(getThis() TSRMLS_CC);
diobj->diff = reltime;
diobj->initialized = 1;
} else {
ZVAL_NULL(getThis());
}
}
zend_restore_error_handling(&error_handling TSRMLS_CC);
}
/* }}} */
似乎都不做date_interval_initialize()
功能:
static int date_interval_initialize(timelib_rel_time **rt, /*const*/ char *format, int format_length TSRMLS_DC)
{
timelib_time *b = NULL, *e = NULL;
timelib_rel_time *p = NULL;
int r = 0;
int retval = 0;
struct timelib_error_container *errors;
timelib_strtointerval(format, format_length, &b, &e, &p, &r, &errors);
if (errors->error_count > 0) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unknown or bad format (%s)", format);
retval = FAILURE;
} else {
if(p) {
*rt = p;
retval = SUCCESS;
} else {
if(b && e) {
timelib_update_ts(b, NULL);
timelib_update_ts(e, NULL);
*rt = timelib_diff(b, e);
retval = SUCCESS;
} else {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Failed to parse interval (%s)", format);
retval = FAILURE;
}
}
}
timelib_error_container_dtor(errors);
return retval;
}
沒有內置的功能來做到這一點,你必須自己創建一個。 如果你不希望覆蓋DateInterval,儘量使用靜態方法:
class DateIntervalUtils{
public static function getSpecString(DateInterval $i){
$stat ="P";
foreach($i as $key=>$value){
if($key !=="days"){
if($key=="h"){
$stat.="T";
}
$stat.=$value.upper($key);
}
}
return $stat;
}
}
如果你想添加的功能(對比等),您可以添加到它。
您可以使用format()函數的工作:
echo $obj->format('P%yY%mM%dDT%hH%iM%sS');
或(String中沒有零值)更好的可讀版本:
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;
}
我不喜歡這個實現。它正在迭代一個對象,除了「天」之外,它還可以包含任何數量的附加字段,並且它已經在PHP 5.4中具有它們。靜態類也是一個壞主意。在PHP中沒有'upper'這樣的函數。 – 2014-08-18 20:28:13