2011-04-30 272 views
0

我有兩個日期字符串合成我想從兩個日期字符串計算這些東西。 1)年差 例如PHP日期差異

$date1 = "20/04/2002"; 
$date2= "20/04/2010"; 
$five_yers_back = <Five years back that is 2005> 

同樣

$date2= "20/05/2010"; 
$a_week = "< seven days back from date2 >"; 

plz幫助

回答

0

使用的strtotime +日期

http://pl2.php.net/manual/en/function.strtotime.php 
http://pl2.php.net/manual/en/function.date.php 
0

我敢肯定有上課要做到這一點您。但作爲C程序員,我更喜歡tm structre的基礎知識。這裏PHP相當於例如

<?php 

$d1 = "20/04/2002"; 
$d1rec = strptime($d1, "%d/%m/%Y"); 
$sec = 0; 
$min = 0; 
$hour = 0; 
$day = $d1rec["tm_mday"]; 
$mon = $d1rec["tm_mon"] + 1; # Because tm_mon is 0-11 
$year = $d1rec["tm_year"]; 
print("DATE: " . strftime("%d/%m/%Y\n" 
       , mktime($hour,$min,$sec,$mon,$day,$year))); 
print("+1WK: " . strftime("%d/%m/%Y\n" 
       , mktime($hour,$min,$sec,$mon,$day+7,$year))); 
print("+2WK: " . strftime("%d/%m/%Y\n" 
       , mktime($hour,$min,$sec,$mon,$day+14,$year))); 
print("+1YR: " . strftime("%d/%m/%Y\n" 
       , mktime($hour,$min,$sec,$mon,$day,$year+1))); 
print("-6MO: " . strftime("%d/%m/%Y\n" 
       , mktime($hour,$min,$sec,$mon-6,$day,$year))); 

?> 

所以基本上

  • 使用strptime到您的字符串解析成一個結構數組
  • 打破數組變量
  • 使用mktime重組(修改後的)變量引入一個time_t整數(並採取每月/每年的打包)
  • 使用strftime返回一個stri ng