PHP 日期加減的類,很不錯
更新時間:2009年10月10日 13:00:57 作者:
PHP中對日期操作的函數(shù)功能強(qiáng)大,下面的代碼是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);
如果一切順利,你可以看到以下結(jié)果:
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;
如果一切順利,你可以看到以下結(jié)果:
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 ;
}
}
?>
復(fù)制代碼 代碼如下:
$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 );
復(fù)制代碼 代碼如下:
$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);
如果一切順利,你可以看到以下結(jié)果:
15h41 Saturday 03 Jun
16h31 Saturday 03 Jun
復(fù)制代碼 代碼如下:
$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 ;
復(fù)制代碼 代碼如下:
$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;
如果一切順利,你可以看到以下結(jié)果:
Current time: 16h23 Saturday 03 Jun
Time plus 50 minutes: 17h13 Saturday 03 Jun
Interval between two times: 50
復(fù)制代碼 代碼如下:
<?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中日期加減法運算實現(xiàn)代碼
- PHP實現(xiàn)針對日期,月數(shù),天數(shù),周數(shù),小時,分,秒等的加減運算示例【基于strtotime】
- php計算兩個日期相差天數(shù)的方法
- PHP中UNIX時間戳和日期間的轉(zhuǎn)換與計算實例
- php計算兩個日期時間差(返回年、月、日)
- 計算一段日期內(nèi)的周末天數(shù)的php代碼(星期六,星期日總和)
- 探討:如何使用PHP實現(xiàn)計算兩個日期間隔的年、月、周、日數(shù)
- php計算到指定日期還有多少天的方法
- php中根據(jù)某年第幾天計算出日期年月日的代碼
- PHP常用日期加減計算方法實例小結(jié)
相關(guān)文章
PHP實現(xiàn)字符串翻轉(zhuǎn)功能的方法【遞歸與循環(huán)算法】
這篇文章主要介紹了PHP實現(xiàn)字符串翻轉(zhuǎn)功能的方法,結(jié)合實例形式對比分析了php使用遞歸與循環(huán)算法實現(xiàn)字符串反轉(zhuǎn)功能的相關(guān)操作技巧,需要的朋友可以參考下2017-11-11
PHP 應(yīng)用程序的安全 -- 不能違反的四條安全規(guī)則
PHP 應(yīng)用程序的安全 -- 不能違反的四條安全規(guī)則...2006-11-11
php allow_url_include的應(yīng)用和解釋
PHP常常因為它可能允許URLS被導(dǎo)入和執(zhí)行語句被人們指責(zé)。事實上,這件事情并不是很讓人感到驚奇,因為這是導(dǎo)致稱為Remote URL Include vulnerabilities的php應(yīng)用程序漏洞的最重要的原因之一。2010-04-04
SublimeText3配置PHP函數(shù)追蹤定位插件
這篇文章主要介紹了SublimeText3的PHP函數(shù)追蹤定位插件ctags和codeBeautifier,對SublimeText3感興趣的同學(xué),可以多研究下2021-04-04
PHP 在5.1.* 和5.2.*之間 PDO數(shù)據(jù)庫操作中的不同之處小結(jié)
今天發(fā)現(xiàn)php5.1.*和php5.2.*在數(shù)據(jù)庫預(yù)編譯代碼執(zhí)行的時候出現(xiàn)差異2012-03-03
深入理解curl類,可用于模擬get,post和curl下載
本篇文章是對curl類,可用于模擬get,post和curl下載進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-06-06

