2012-04-14 88 views
1

如何計算已經從當前YEAR到今天的星期日數。我也想計算已經從當前MONTH到今天的星期日數量。計算當前年份和當前月份的星期日數量?

today is 14 April 2012 I should get 2 for the Number of Sundays 
that are passed from the current month. 

誰能給我一個提示或教程如何可以做到這一點?

+2

看看這個http://stackoverflow.com/questions/336127/calculate-business-days。你可以在那裏找到你需要的一切。 – kpotehin 2012-04-14 10:37:53

回答

5

嗯,我想這是很簡單的使用功能date()

//will give you the amount of sundays from the begining of the year 
$daysTotal = ceil((date("z") - date("w"))/7); 
//will give you the amount of sundays from the begining of the month 
$daysTotal = ceil((date("j") - date("w"))/7); 

我沒有測試它,你可能要檢查輪()函數的工作權在這種情況下,但該點被傳遞我想

祝你好運

+0

非常感謝主席先生 – ScoRpion 2012-04-14 13:18:58

0

日期('W')可能會幫助你。檢查PHP手冊的格式 - http://php.net/manual/en/function.date.php

不要忘記根據手冊 - ISO-8601週數,週數從星期一開始。 因此,即使這不是星期天,本週也會計算在內。所以在這種情況下減少數量1.

function get_sunday_since_year_start($today=null) 
{ 
    if($today==null) $today = time(); 

    if(date('D',$today)=='Sun') 
    return Date('W',$today); 
    else 
    return Date('W',$today)-1; 
} 

function get_sunday_since_month_start() 
{ 
    return get_sunday_since_year_start()-get_sunday_since_year_start(strtotime(date('Y-m-01 00:00:00'))); 
} 
相關問題