Skip to content

Instantly share code, notes, and snippets.

@matishsiao
Last active August 29, 2015 14:28
Show Gist options
  • Save matishsiao/bf3bfa9f13c9eaff2750 to your computer and use it in GitHub Desktop.
Save matishsiao/bf3bfa9f13c9eaff2750 to your computer and use it in GitHub Desktop.
Go time format like PHP time format
package main
import (
"fmt"
"strings"
"time"
)
func main() {
date := GetDate("Y-m-d H:I:s")
fmt.Println("date:",date)
}
func GetDate(date string) string {
t := time.Now()
timestr := t.Format("2006,06,01,1,02,2,15,04,4,05")
timearr := strings.Split(timestr,",")
keys := []string{"Y","y","M","m","D","d","H","I","i","s"}
timeinfo := make(map[string]string)
for k,v := range timearr {
timeinfo[keys[k]] = v
}
tmpstr := date
tmpstr = strings.Replace(tmpstr,"Y",timeinfo["Y"],-1)
tmpstr = strings.Replace(tmpstr,"y",timeinfo["y"],-1)
tmpstr = strings.Replace(tmpstr,"M",timeinfo["M"],-1)
tmpstr = strings.Replace(tmpstr,"m",timeinfo["m"],-1)
tmpstr = strings.Replace(tmpstr,"D",timeinfo["D"],-1)
tmpstr = strings.Replace(tmpstr,"d",timeinfo["d"],-1)
tmpstr = strings.Replace(tmpstr,"H",timeinfo["H"],-1)
tmpstr = strings.Replace(tmpstr,"I",timeinfo["I"],-1)
tmpstr = strings.Replace(tmpstr,"i",timeinfo["i"],-1)
tmpstr = strings.Replace(tmpstr,"s",timeinfo["s"],-1)
return tmpstr
}
//Result:
//date: 2009-11-10 23:00:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment