2010年1月12日 星期二

PHP中計算某一周的起始及結束日期

以星期日為一周起始,函式傳入年份及第幾周,函式傳出開始及結束日期。


function findWeekPeriod( $week, $year )
{
    $aPeriod = array();
    $first_day = strtotime($year."-01-01");
    $is_week_first_day = date("w", $first_day) == 0;
    $is_weekone = strftime("%V", $first_day) == 1;
    if($is_weekone)
    {
        $week_one_start = $is_week_first_day ? strtotime("last sunday",$first_day) : $first_day;
    }
    else
    {
        $week_one_start = strtotime("next sunday", $first_day);
    }
    $aPeriod['start'] =  date("Y年m月d日", $week_one_start+(3600*24*7*($week-1)) );
    $aPeriod['end'] =  date("Y年m月d日", $week_one_start+(3600*24*(7*$week-1)) );
    return $aPeriod;      
}

例如…
      $week=date('W');
      $year=date('Y');
      $wPeriod=findWeekPeriod( $week, $year );
      /// 傳回當周的起始結束日期。

沒有留言:

更高效處理 micro second 的方式

更高效處理 micro second 的方式…  以 STM32 為例… __IO unsigned long sys_tick = 0; void SysTick_Handler(void) {     HAL_IncTick();     sys_tick += (SysTi...