2012-03-26 73 views
0

也許我做錯了什麼,但是我看不到它的重點......php date&strtotime

今天是一週中的某一天。我必須知道 - 從上週一到今天有多少天?

$date_1d = date('Y-m-d', strtotime ('last Monday'));      
// last monday 

$date_today = date('Y-m-d');            
// actual date, today 

$ile_dni = (strtotime($date_today) - strtotime($date_1d))/(60*60*24); 
// difference in days - how many days have elapsed from today to last monday 

我有2個相同的腳本如以上在同一目錄中的同一服務器

first said: 

&date_1d = > 2012-03-19 

$date_today => 2012-03-26 

strtotime($date_today) = > 1332720000 

strtotime($date_1d) = > 1332115200 

$ile_dni = > 7 

second said: 

&date_1d = > 2012-03-19 

$date_today => 2012-03-26 

strtotime($date_today) = > 1332712800 

strtotime($date_1d) = > 1332111600 

$ile_dni = > 6.9583333333333 

:原因正確的答案是第一上。以及如何處理這種情況?

+3

最新日期('N')是什麼錯? – Vytautas 2012-03-26 08:03:39

回答

1

$ile_dni的差異使用ceil()從「上週一出產生的時間戳和的strtotime,我敢肯定有一個更好的方式與date('N')做到這一點,你可以做

$ile_dni = date('N')-1; 

如果你想星期一ile_dni爲0,星期二等1。

+0

thnks,現在我會嘗試與單元格(),看看是什麼將會。 thnks – Andrew 2012-03-26 13:10:10

1

你是否同時運行這兩個腳本?我猜測有一個時區/ DST問題正在進行。請注意,您計算日期差異的方法非常容易受到這類問題的影響。如果您使用PHP 5.3或更高版本,請使用date_diff

+0

thnks你的幫助 – Andrew 2012-03-26 13:10:51