Go获取当日开始和结束的Unix时间戳

package main

import (
	"fmt"
	"time"
)

func main() {
	startUnix,endUnix := GetDateTime()
	fmt.Println(startUnix,endUnix)
}


func GetDateTime() (int64,int64) {
	//1.获取当前时区
	loc, _ := time.LoadLocation("Local")

	//2.今日日期字符串
	date := time.Now().Format("2006-01-02")

	//3.拼接成当天0点时间字符串
	startDate := date + " 00:00:00"
	//得到0点日期 2021-04-24 00:00:00 +0800 CST
	startTime, _ := time.ParseInLocation("2006-01-02 15:04:05", startDate, loc)

	//4.拼接成当天23点时间字符串
	endDate := date + " 23:59:59"
	//得到23点日期 2021-04-24 23:59:59 +0800 CST
	endTime, _ := time.ParseInLocation("2006-01-02 15:04:05", endDate, loc)

	//5.返回当天0点和23点59分的时间戳
	return startTime.Unix(), endTime.Unix()
}


简写如下

	startUnix := time.Date(time.Now().Year(), time.Now().Month(), time.Now().Day(), 0, 0, 0, 0, time.Now().Location()).Unix()
	endUnix := startUnix + 86400 - 1

PHP

$beginToday=mktime(0,0,0,date('m'),date('d'),date('Y'));
$endToday=mktime(0,0,0,date('m'),date('d')+1,date('Y'))-1;

你可能感兴趣的:(Go,go,php)