PHP日期 - 从格式W-m-Y获取给定周的开始和结束日期

时间:2016-10-21 11:49:43

标签: php date

我有一系列格式为W-m-Y的日期。

例如34-08-2016我希望获得20-08-2016 - 26-08-2016之类的内容。那些请求格式的日子并不真实。

知道如何解决这个问题吗?

5 个答案:

答案 0 :(得分:3)

试试这个

function getStartAndEndDate($week, $year) {
  $dto = new DateTime();
  $dto->setISODate($year, $week);
  $ret['week_start'] = $dto->format('Y-m-d');
  $dto->modify('+6 days');
  $ret['week_end'] = $dto->format('Y-m-d');
  return $ret;
}

$week_array = getStartAndEndDate(34,2016);

echo "start date ".date('d-m-Y',strtotime($week_array['week_start'])).'<br>';
echo "End date ".date('d-m-Y',strtotime($week_array['week_end']));

答案 1 :(得分:1)

试试这个,

function getWeekDates($year, $week)
{
    $from = date("Y-m-d", strtotime("{$year}-W{$week}-1")); //Returns the date of monday in week
    $to = date("Y-m-d", strtotime("{$year}-W{$week}-7"));   //Returns the date of sunday in week     
    return  $from ." - ". $to;        
    //return "Week {$week} in {$year} is from {$from} to {$to}.";
}
$year = 2016;   
$week = '34';
echo getWeekDates($year, $week);

<强> DEMO

答案 2 :(得分:1)

试试这个,希望这有帮助......,你也可以优化它..

$date = "34-08-2016";
list($week_no, $month, $year) = explode("-", $date);
$date_obj = new DateTime();
$date_obj->setISODate($year,$week_no);
$day = $date_obj->format('w');
$week_start = date('m-d-Y', strtotime('-'.$day.' days', strtotime($date_obj->format('Y-m-d'))));
$week_end = date('m-d-Y', strtotime('+'.(6-$day).' days', strtotime($date_obj->format('Y-m-d'))));

答案 3 :(得分:0)

试试这个

class MenuItemView : LinearLayout {

@BindView(R.id.menu_title_text_view_id)
lateinit var menuTitleTextView : CTextBasic

constructor(ctx: Context) : super(ctx) {
}

init {
    val view = LayoutInflater.from(context).inflate(R.layout.menu_item,this)
    ButterKnife.bind(this,view)
}

constructor(ctx: Context, attrs: AttributeSet) : super(ctx, attrs) {
    val menuAttrs = context.theme.obtainStyledAttributes(attrs, R.styleable.MenuItemView, 0, 0)
    try {
        val title: String = menuAttrs.getString(R.styleable.MenuItemView_menu_title)
        menuTitleTextView.text = title
    }catch (e : Exception){
        e.printStackTrace()
    }finally {
        menuAttrs.recycle()
    }
}
fun setTitle( title : String){
    menuTitleTextView.text = title
}
}

这将收集您给出的日期的周数,并查找该周的周一和周日。

答案 4 :(得分:0)

你可以试试这个

function date_of_week($date)
{
    echo date("d-m-Y",$date)."\n";

    $day_of_week = date('N', $date); # 0->sunday, 1-> monday etc...
    echo "\tday of week:$day_of_week\n";
    #I assume you begin the week on monday.
    $start_week_date = $date - ($day_of_week-1)*3600*24;
    $end_week_date = $start_week_date + 6*3600*24;

    return date('d-m-Y', $start_week_date)." - ".date('d-m-Y', $end_week_date);

}


for($i=16;$i<24;$i++)
{
    echo date_of_week(mktime(0,0,0,10,$i,2016))."\n\n";
}