php inteval,DateInterval

本文介绍了一个PHP类MyDateInterval,该类继承自标准的DateInterval类,并提供了一个formatWithoutZeroes方法,用于格式化日期间隔,忽略为零的时间单位,以更自然的方式展示时间差。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

error_reporting(E_ALL);

classMyDateIntervalextendsDateInterval{

public$pluralCheck='()',// Must be exactly 2 characters long

// The first character is the opening brace, the second the closing brace

// Text between these braces will be used if > 1, or replaced with $this->singularReplacement if = 1$singularReplacement='',// Replaces $this->pluralCheck if = 1

// hour(s) -> hour$separator=', ',// Delimiter between units

// 3 hours, 2 minutes$finalSeparator=', and ',// Delimeter between next-to-last unit and last unit

// 3 hours, 2 minutes, and 1 second$finalSeparator2=' and ';// Delimeter between units if there are only 2 units

// 3 hours and 2 minutespublic static functioncreateFromDateInterval(DateInterval $interval) {$obj= newself('PT0S');

foreach ($intervalas$property=>$value) {$obj->$property=$value;

}

return$obj;

}

public functionformatWithoutZeroes() {// Each argument may have only one % parameter

// Result does not handle %R or %r -- but you can retrieve that information using $this->format('%R') and using your own logic$parts= array ();

foreach (func_get_args() as$arg) {$pre=mb_substr($arg,0,mb_strpos($arg,'%'));$param=mb_substr($arg,mb_strpos($arg,'%'),2);$post=mb_substr($arg,mb_strpos($arg,$param)+mb_strlen($param));$num=intval(parent::format($param));$open=preg_quote($this->pluralCheck[0],'/');$close=preg_quote($this->pluralCheck[1],'/');$pattern="/$open(.*)$close/";

list ($pre,$post) =preg_replace($pattern,$num==1?$this->singularReplacement:'$1', array ($pre,$post));

if ($num!=0) {$parts[] =$pre.$num.$post;

}

}$output='';$l=count($parts);

foreach ($partsas$i=>$part) {$output.=$part.($iseparator: ($l==2?$this->finalSeparator2: ($i==$l-2?$this->finalSeparator:'')));

}

return$output;

}

}date_default_timezone_set('America/Phoenix');$today= newDateTime('today');

echo'Today is ',$today->format('F d, Y h:ia'),'.
',PHP_EOL;// Today is August 28, 2013 12:00am.
$expiration= newDateTime('today +1 year +2 months +16 days +1 minute +15 seconds');

echo'Expires ',$expiration->format('F d, Y h:ia'),'.
',PHP_EOL;// Expires November 13, 2014 12:01am.
$interval=MyDateInterval::createFromDateInterval($today->diff($expiration));

echo'That is ',$interval->formatWithoutZeroes('%y year(s)','%m month(s)','%d day(s)','%h hour(s)','%i minute(s)','%s second(s)'),' from now.
',PHP_EOL;// That is 1 year, 2 months, 16 days, 1 minute, and 15 seconds from now.$interval->finalSeparator=', y ';$interval->finalSeparator2=' y ';

echo'Que es de ',$interval->formatWithoutZeroes('%y a?o(s)','%m mes(es)','%d d??a(s)','%h hora(s)','%i minuto(s)','%s segundo(s)'),' a partir de ahora.';// Que es de 1 a?o, 2 meses, 16 d??as, 1 minuto, y 15 segundos a partir de ahora.

// Is that correct? Spanish isn't my strength....?>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值