내가 DateInterval의 객체 인스턴스가 말 :DateInterval 객체에서 "interval_spec"문자열을 가져올 수 있습니까?
$obj=new DateInterval("P1Y12D");
지금 내가 그 $obj
인스턴스와 몇 꽤 일을 할 수 있지만이 개체에서 "P1Y12D"
문자열 것을 얻고 싶은 말은하지 않고 바로 가능하다 수업을 무시해야합니까?
나는이 방법을 찾지 못했지만 어쩌면 그렇게 할 수 있습니다.
내가 DateInterval의 객체 인스턴스가 말 :DateInterval 객체에서 "interval_spec"문자열을 가져올 수 있습니까?
$obj=new DateInterval("P1Y12D");
지금 내가 그 $obj
인스턴스와 몇 꽤 일을 할 수 있지만이 개체에서 "P1Y12D"
문자열 것을 얻고 싶은 말은하지 않고 바로 가능하다 수업을 무시해야합니까?
나는이 방법을 찾지 못했지만 어쩌면 그렇게 할 수 있습니다.
현재 PHP 커뮤니티 및 PSR 요구 사항에 따라 향상되었습니다. 나는 또한 그것을 더 읽기 쉽고 똑 바르게 만들기 위해 노력했다. 내가이 사람을 도움이되기를 바랍니다
$interval = new CustomDateInterval('P1Y2M3DT4H5M6S');
// Prints "P1Y2M3DT4H5M6S".
print $interval . PHP_EOL;
:
이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;
}
}
그것은 다음과 같이 사용할 수 있습니다
/**
* @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
클래스에 대한 확장입니다 , 환호!
은 내가 C 전문가가 아니에요하지만 생성자 함수의 소스 코드의 값은 전혀 저장하지 않는 것 :
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;
}
:
/* {{{ 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()
기능을 수행하는 것
내장 된 기능이 없으므로 혼자서 만들어야합니다. 당신이 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;
}
}
당신이 (등을 비교) 기능을 추가 할 경우 여기에 추가 할 수 있습니다.
당신은 형식() 함수를 사용하여 작업 할 수 있습니다
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;
}
이 구현이 마음에 들지 않습니다. "days"외에도 미래에 추가 필드를 가질 수있는 객체를 반복하고 PHP 5.4에 이미 포함되어 있습니다. 정적 클래스도 나쁜 생각입니다. PHP에는'upper' 같은 함수가 없습니다. –