2009/08/05

日期操作函數 for PHP

(撰寫於 2009-01-12 )

不需要用到 PHP5 的 DateTime class,在 PHP 4 的環境就可以 run
出處:PHP Free.net


<?php
/* ----------------------------------------
Interval can be one of:
yyyy year
q Quarter
m Month
y Day of year
d Day
w Weekday
ww Week of year
h Hour
n Minute
s
Second
---------------------------------------- */
function
DateAdd ($interval, $number, $date) {
$date_time_array = getdate($date); $date_time_array = getdate($date);
$hours = $date_time_array["hours"]; $hours = $date_time_array["hours"];
$minutes = $date_time_array["minutes"]; $minutes = $date_time_array["minutes"];
$seconds = $date_time_array["seconds"]; $seconds = $date_time_array["seconds"];
$month = $date_time_array["mon"]; $month = $date_time_array["mon"];
$day = $date_time_array["mday"]; $day = $date_time_array["mday"];
$year = $date_time_array["year"]; $year = $date_time_array["year"];
switch ($interval) {
case "yyyy": $year += $number; break; case "yyyy": $year += $number; break;
case "q": $month += ($number*3); break; case "q": $month += ($number*3); break;
case "m": $month += $number; break; case "m": $month += $number; break;
case "y": case "y":
case "d": case "d":
case "w": $day += $number; break; case "w": $day += $number; break;
case "ww": $day += ($number*7); break; case "ww": $day += ($number*7); break;
case "h": $hours += $number; break; case "h": $hours += $number; break;
case "n": $minutes += $number; break; case "n": $minutes += $number; break;
case "s": $seconds += $number; break; case "s": $seconds += $number; break;
}
$timestamp = mktime($hours ,$minutes, $seconds,$month ,$day, $year);
return $timestamp;
}
?>

沒有留言:

張貼留言