获取星期一星期二等 [英] Get mondays tuesdays etc

查看:51
本文介绍了获取星期一星期二等的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何在这两个日期之间回显星期一/星期三/星期五的所有日期?

How can I echo all dates for mondays/ wednesday / friday between these two dates?

这就是我一直在使用的:

This is what I have been working with:

    function get_days ( $s, $e )
{
    $r = array ();
    $s = strtotime ( $s . ' GMT' );
    $e = strtotime ( $e . ' GMT' );

    $day = gmdate ( 'l', $s );

    do
    {
        $r[] = $day . ', ' . gmdate ( 'Y-m-d', $s );

        $s += 86400 * 7;

    } while ( $s <= $e );

    return $r;
}

print_r ( get_days ( $start, $end ) );


推荐答案

尝试一下:

function getDates($start_date, $end_date, $days){

    // parse the $start_date and $end_date string values
    $stime=new DateTime($start_date);
    $etime=new DateTime($end_date);

    // make a copy so we can increment by day    
    $ctime = clone $stime;
    $results = array();
    while( $ctime <= $etime ){

        $dow=$ctime->format("w");
        // assumes $days is array containing integers for Sun (0) - Sat (6)
        if( in_array($dow, $days) ){ 
            // make a copy to return in results
            $results[]=clone $ctime;
        }
        // incrememnt by 1 day
        //$ctime=date_add($ctime, date_interval_create_from_date_string('1 days'));
        $ctime->modify("+1 days");
    }

    return $results;
}

// get every Tues, Wed, Fri from now to End of June
getDates('2011-06-15','2011-06-30',array(2,3,5));

这篇关于获取星期一星期二等的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆