2014-03-12 80 views
2

我想找到最近的前一個星期日到一個日期(除非日期是星期天)。找到最近的星期日?

因此,舉例來說,如果我有這樣的數組:

$dates = array(
    "2014-03-02 10:15:10", // sun 
    "2014-03-03 12:15:10", // mon 
    "2014-03-04 13:15:10", // tue 
    "2014-03-05 10:15:10", // wed 
    "2014-03-06 14:15:10", // thu 
    "2014-03-07 18:15:10", // fri 
    "2014-03-08 14:15:10", // sat 
    "2014-03-09 14:15:10" // sun 
); 

我怎樣才能高效地輸出這樣的:

$dates = array(
    "2014-03-02 00:00:00", // sun 
    "2014-03-02 00:00:00", // sun 
    "2014-03-02 00:00:00", // sun 
    "2014-03-02 00:00:00", // sun 
    "2014-03-02 00:00:00", // sun 
    "2014-03-02 00:00:00", // sun 
    "2014-03-02 00:00:00", // sun 
    "2014-03-09 00:00:00" // sun 
); 
+0

該問題已關閉,但我正在研究我的回答:http://pastie.org/8912529。它比所有其他 – Qualcuno

+0

我認爲有一個更簡單的方法不同:'$日期=新的日期時間($ dateFromYourArray);''$日期 - >修改(0 - $日期 - >格式( 'W') '天'。); ''$ date-> setTime(0,0,0);' –

回答

2

this answer

$date = date('Y-m-d', strtotime('last Sunday', strtotime($date))); 

在你的c ase:

$dates = array_map('find_sunday', $dates); 

function find_sunday($date) { 
    if (date('w', strtotime($date)) == 0) { 
     return date('Y-m-d', strtotime($date)); 
    } 
    return date('Y-m-d', strtotime('last Sunday', strtotime($date))); 
} 
+0

thx ...但是當日期被檢查的日期是星期日時不起作用。 – user2217162

+0

THX ...還是不工作;) – user2217162

+0

其輸出它到底是什麼之前輸出 – user2217162