2014-03-12 75 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