我敢肯定有上課要做到這一點您。但作爲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