PHP 日期加减的类,很不错

yipeiwu_com5年前PHP代码库
如何使用这个类呢?请看下面的演示:
复制代码 代码如下:

$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 ;
}
}
?>

相关文章

解析PHP中的正则表达式以及模式匹配

PHP中对于正则处理文本提供了两种方式,一种是PCRE方式(PCRE库是一个实现了与perl 5在语法和语义上略有差异(详见下文)的正则表达式模式匹配功能的函数集. 当前的实现对应于pe...

一个严格的PHP Session会话超时时间设置方法

最近某个PHP项目用到了限制登录时间的功能,比如用户登录系统60分钟后如果没有操作就自动退出,我搜索了网络收集了有以下方法可供参考。 第一种方法即设置php.ini配置文件,设置sess...

PHP学习笔记(二) 了解PHP的基本语法以及目录结构

PHP学习笔记(二) 了解PHP的基本语法以及目录结构

通过这节课, 1.了解AppServ的目录结构 2.我们可以了解PHP的基本语法结构 1 我的AppServ安装目录是E盘: ①运用命令行的方式去操作apache服务器 apache服...

php变量范围介绍

例如: 复制代码 代码如下: <?php $a = 1; include 'b.inc'; ?> 这里变量 $a 将会在包含文件 b.inc 中生效。但是,在用户自定义函数...

php中session_unset与session_destroy的区别分析

session_unset() 释放当前在内存中已经创建的所有$_SESSION变量,但不删除session文件以及不释放对应的session id session_destroy()...