程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 網頁編程 >> PHP編程 >> 關於PHP編程 >> PHP 日期加減的類,很不錯

PHP 日期加減的類,很不錯

編輯:關於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 ;
}
}
?>

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved