PHP相当于GO中的strtotime()

时间:2016-08-27 08:28:29

标签: go

我正在尝试将日期/时间字符串转换为unix时间戳字符串:

<?php
    echo strtotime("20140921040000");
?>
Output: 1411286400   //timestamp

在GO语言中执行相同操作,但未获得所需结果。 Go代码如下:

package main

import (
    "fmt"
    "time"
)

func main() {
    tm := time.Unix(1411286400, 0)
    fmt.Println(tm) //output: 2014-09-21 08:00:00 +0000 UTC

    //================
    layout := "20060102150405"
    str := "20140921040000"
    t, err := time.Parse(layout, str)
    if err != nil {
        fmt.Println(err)
    }
    fmt.Println(t) // output: 2014-09-21 04:00:00 +0000 UTC
}

1 个答案:

答案 0 :(得分:3)

在你的最后一行中:

fmt.Println(t.Unix())

您可以将其与此PHP代码进行比较:http://sandbox.onlinephpfunctions.com/code/b520953ea26e2d84ed85db6f5657ceeccade08d4