2009-11-06 116 views
3

我想要一個星期的第一天說我有第45周。現在我想有本週的第一個星期日的日期。 任何人都可以告訴我如何去做這件事?一週的第一天

謝謝

回答

4

張望了一下搜索,最建議是:

$year = "2009"; // date("Y"); 
$week = "45"; // date("W"); 

$firstDayOfWeek = strtotime($year."W".str_pad($week,2,"0",STR_PAD_LEFT)); 

print("The first day of week ".$week." of ".$year." is ".date("D, d-m-Y",$firstDayOfWeek)); 

這基本上可以歸結爲讓的strtotime做的工作適合你。你填寫「2009W45」,它會爲你檢索日期。 這裏的缺點是,本週需要採用2位數字格式。因此,第1周需要爲01,因此str_pad將其填充爲零。

+0

'2009W45' =>我只需要這種格式!謝謝! :d – 2015-08-07 07:07:50

1

你應該strtotimedate功能

$next_sunday = strtotime('next sunday'); 
$next_week = strtotime('+1 week'); 
$next_sunday_of_next_week = strtotime('next sunday', $next_week); 

希望一些代碼,這有助於

0

如果您有月,日和年:

$timestamp = mktime(0, 0, 0, $month, $day, $year); 
echo date('c', $timestamp) = mktime(0, 0, 0, $month, date('d', $timestamp)-date('w', $timestamp), $year); 

你也可以做到這樣,如果你有時間戳:

echo $date('c', mktime(0, 0, 0, $month, date('d', $timestamp)-date('w', $timestamp), $year)); 

來源:http://pinoytech.org/blog/post/get-the-first-day-of-the-week-with-an-exact-date

0

date($str_format, strtotime($year."W".$week."1"))

哪裏$str_format是根據PHP date()function輸出甲..我使用'M d Y'

+0

如果您將星期一作爲第一週,則適用。 – 2009-11-06 06:15:10

-1

如果您能向我們提供更多的細節,您希望以何種語言達到此目的,我們只能提供幫助,明確指出這需要在任何語言,腳本或數據庫中完成?

+1

問題標籤爲php。 – notJim 2009-11-06 06:16:56