PHP 日期加减的类,很不错
程序员文章站
2023-11-09 22:40:28
如何使用这个类呢?请看下面的演示: 复制代码 代码如下: $temptime = time(); echo strftime ( "%hh%m %a %d %b" , $t...
如何使用这个类呢?请看下面的演示:
$temptime = time();
echo strftime ( "%hh%m %a %d %b" , $temptime );
$date = new dateaccount();
$temptime = $date ->dateadd( "n" ,50, $temptime );
echo "<p>" ;
echo strftime ( "%hh%m %a %d %b" , $temptime );
$temptime = time();
echo strftime( "%hh%m %a %d %b",$temptime);
$date = new dateaccount();
$temptime = $date->dateadd("n" ,50,$temptime);
echo "<p>";
echo strftime( "%hh%m %a %d %b",$temptime);
如果一切顺利,你可以看到以下结果:
15h41 saturday 03 jun
16h31 saturday 03 jun
$currenttime = time();
echo "current time: " . strftime ( "%hh%m %a %d %b" , $currenttime ). "<br>" ;
$date = new dateaccount();
$newtime = $date ->dateadd ( "n" ,50 , $currenttime );
echo "time plus 50 minutes: " . strftime ( "%hh%m %a %d %b" , $newtime ). "<br>" ;
$temptime = $date ->datediff ( "n" , $currenttime , $newtime );
echo "interval between two times: " . $temptime ;
$currenttime = time();
echo "current time: ". strftime("%hh%m %a %d %b" ,$currenttime)."<br>";
$date = new dateaccount();
$newtime = $date->dateadd ("n",50 ,$currenttime);
echo "time plus 50 minutes: ". strftime("%hh%m %a %d %b" ,$newtime)."<br>";
$temptime = $date->datediff ("n",$currenttime ,$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
<?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 ;
}
}
?>
复制代码 代码如下:
$temptime = time();
echo strftime ( "%hh%m %a %d %b" , $temptime );
$date = new dateaccount();
$temptime = $date ->dateadd( "n" ,50, $temptime );
echo "<p>" ;
echo strftime ( "%hh%m %a %d %b" , $temptime );
复制代码 代码如下:
$temptime = time();
echo strftime( "%hh%m %a %d %b",$temptime);
$date = new dateaccount();
$temptime = $date->dateadd("n" ,50,$temptime);
echo "<p>";
echo strftime( "%hh%m %a %d %b",$temptime);
如果一切顺利,你可以看到以下结果:
15h41 saturday 03 jun
16h31 saturday 03 jun
复制代码 代码如下:
$currenttime = time();
echo "current time: " . strftime ( "%hh%m %a %d %b" , $currenttime ). "<br>" ;
$date = new dateaccount();
$newtime = $date ->dateadd ( "n" ,50 , $currenttime );
echo "time plus 50 minutes: " . strftime ( "%hh%m %a %d %b" , $newtime ). "<br>" ;
$temptime = $date ->datediff ( "n" , $currenttime , $newtime );
echo "interval between two times: " . $temptime ;
复制代码 代码如下:
$currenttime = time();
echo "current time: ". strftime("%hh%m %a %d %b" ,$currenttime)."<br>";
$date = new dateaccount();
$newtime = $date->dateadd ("n",50 ,$currenttime);
echo "time plus 50 minutes: ". strftime("%hh%m %a %d %b" ,$newtime)."<br>";
$temptime = $date->datediff ("n",$currenttime ,$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
复制代码 代码如下:
<?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 ;
}
}
?>