微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

PHP 日期加减的类,很不错

如何使用这个类呢?请看下面的演示:
<div class="codetitle"><a style="CURSOR: pointer" data="31415" class="copybut" id="copybut31415" onclick="doCopy('code31415')"> 代码如下:

<div class="codebody" id="code31415">
$temptime = time();
echo strftime ( "%Hh%M %A %d %b",$temptime );
$date = new DateAccount();
$temptime = $date ->DateAdd( "n",50,$temptime );
echo "

" ;
echo strftime ( "%Hh%M %A %d %b",$temptime );


<div class="codetitle"><a style="CURSOR: pointer" data="23129" class="copybut" id="copybut23129" onclick="doCopy('code23129')"> 代码如下:
<div class="codebody" id="code23129">
$temptime = time();
echo strftime( "%Hh%M %A %d %b",$temptime);
$date = new DateAccount();
$temptime = $date->DateAdd("n",$temptime);
echo "

";
echo strftime( "%Hh%M %A %d %b",$temptime);


如果一切顺利,你可以看到以下结果:
15h41 Saturday 03 Jun
16h31 Saturday 03 Jun
<div class="codetitle"><a style="CURSOR: pointer" data="1103" class="copybut" id="copybut1103" onclick="doCopy('code1103')"> 代码如下:
<div class="codebody" id="code1103">
$currenttime = time();
echo "Current time: " . strftime ( "%Hh%M %A %d %b",$currenttime ). "
" ;
$date = new DateAccount();
$newtime = $date ->DateAdd ( "n",$currenttime );
echo "Time plus 50 minutes: " . strftime ( "%Hh%M %A %d %b",$newtime ). "
" ;
$temptime = $date ->DateDiff ( "n",$currenttime,$newtime );
echo "Interval between two times: " . $temptime ;

<div class="codetitle"><a style="CURSOR: pointer" data="40624" class="copybut" id="copybut40624" onclick="doCopy('code40624')"> 代码如下:
<div class="codebody" id="code40624">
$currenttime = time();
echo "Current time: ". strftime("%Hh%M %A %d %b",$currenttime)."
";
$date = new DateAccount();
$newtime = $date->DateAdd ("n",$currenttime);
echo "Time plus 50 minutes: ". strftime("%Hh%M %A %d %b",$newtime)."
";
$temptime = $date->DateDiff ("n",$newtime);
echo "Interval between two times: ".$temptime;

如果一切顺利,你可以看到以下结果:
Current time: 16h23 Saturday 03 Jun
Time plus 50 minutes: 17h13 Saturday 03 Jun
Interval between two times: 50
<div class="codetitle"><a style="CURSOR: pointer" data="22061" class="copybut" id="copybut22061" onclick="doCopy('code22061')"> 代码如下:<div class="codebody" id="code22061">
<?PHP
class DateAccount{
function __construct(){
}
function DateAdd ( $interval,$number,$date ) {
$date_time_array = getdate ( $date );
$hours = $date_time_array [ "hours" ];
$minutes = $date_time_array [ "minutes" ];
$seconds = $date_time_array [ "seconds" ];
$month = $date_time_array [ "mon" ];
$day = $date_time_array [ "mday" ];
$year = $date_time_array [ "year" ];
switch ( $interval ) {
case "yyyy" : $year += $number ; break ;
case "q" : $month +=( $number 3); break ;
case "m" : $month += $number ; break ;
case "y" :
case "d" :
case "w" : $day += $number ; break ;
case "ww" : $day +=( $number
7); break ;
case "h" : $hours += $number ; break ;
case "n" : $minutes += $number ; break ;
case "s" : $seconds += $number ; break ;
}
$timestamp = mktime ( $hours,$minutes,$seconds,$month,$day,$year );
return $timestamp ;
}
function DateDiff ( $interval,$date1,$date2 ) {
$timedifference = $date2 - $date1 ;
switch ( $interval ) {
case "w" : $retval = bcdiv ( $timedifference,604800); break ;
case "d" : $retval = bcdiv ( $timedifference,86400); break ;
case "h" : $retval = bcdiv ( $timedifference,3600); break ;
case "n" : $retval = bcdiv ( $timedifference,60); break ;
case "s" : $retval = $timedifference ; break ;
}
return $retval ;
}
}
?>

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐