PHP试题网_中国最大的免费网络PHP试题测试平台,PHP试卷调查,PHP试卷模板、PHP考试测验
公告: 【20201211】公告:复杂试卷成功上线,问答模块很快上线……网站将有诸多变化




取上月/周开始、结束时间:

func get_lastmonth_begin_end() (map[string]interface{} ) {
    
   now := time.Now()
   lastMonthFirstDay := now.AddDate(0, -1, -now.Day()+1)//利用此特性,可以得到上周 now.AddDate(0, 0, -int(now.Weekday())-6)的开始时间
   lastMonthStart := time.Date(lastMonthFirstDay.Year(), lastMonthFirstDay.Month(), lastMonthFirstDay.Day(), 0, 0, 0, 0, now.Location()).Unix()
   lastMonthEndDay := lastMonthFirstDay.AddDate(0, 1, -1)
   lastMonthEnd := time.Date(lastMonthEndDay.Year(), lastMonthEndDay.Month(), lastMonthEndDay.Day(), 23, 59, 59, 0, now.Location()).Unix()

	
    ret := make(map[string]interface{})
    ret["time_begin_format"] = time.Unix(lastMonthStart, 0).Format("2006-01-02 15:04:05")
    ret["time_end_format"]   = time.Unix(lastMonthEnd, 0).Format("2006-01-02 15:04:05")
	
 
    ret["time_begin"]        = lastMonthStart
    ret["time_end"]          = lastMonthEnd
	
    return ret 
}
执行结果:
map[time_begin_format:2020-12-01 00:00:00 time_end_format:2020-12-31 23:59:59 time_begin:1606752000 time_end:1609430399]




取本周一,周末时间:

func get_weekday_begin_end() (map[string]interface{} ) {
    now := time.Now()

	offset := int(time.Monday - now.Weekday())
 
	weekStartDate := time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, time.Local).AddDate(0, 0, offset)
	weekMonday := weekStartDate.Format("2006-01-02")

	TimeMonday, _ := time.Parse("2006-01-02", weekMonday)
	lastWeekMonday := TimeMonday.AddDate(0, 0, 6)
	enddate := time.Date(lastWeekMonday.Year(), lastWeekMonday.Month(), lastWeekMonday.Day(), 23, 59, 59, 0, time.Local)
	end := enddate.Format("2006-01-02 15:04:05")
	
	
	ret := make(map[string]interface{})
	ret["time_begin_format"] = weekMonday
	ret["time_end_format"]  = end
	
 
	ret["time_begin"]    = weekStartDate.Unix()
	ret["time_end"]     = enddate.Unix()
	
	return ret 
}

执行结果:
map[time_begin_format:2021-01-18 time_end_format:2021-01-24 23:59:59 time_begin:1610899200 time_end:1611503999]



作者:OK兄 浏览次数:11