mktime
mktime(int $hour, ?int $minute = null, ?int $second = null, ?int $month = null, ?int $day = null, ?int $year = null): int|false
Builds a Unix timestamp from year, month, day, hour, minute, and second.
C C23
C23
#include <stdio.h>
#include <time.h>
int main(void) {
struct tm t = {0}; // zero initialization
t.tm_year = 2020 - 1900; // years are counted from 1900
t.tm_mon = 0; // months are 0-based
t.tm_mday = 2;
t.tm_hour = 3;
t.tm_min = 4;
t.tm_sec = 5;
// timegm interprets tm as UTC (mktime uses local time)
printf("%lld\n", (long long)timegm(&t));
return 0;
}
Output
1577934245
C++ C++26
C++26
#include <cstdio>
#include <ctime>
int main() {
std::tm t = {};
t.tm_year = 2020 - 1900; // years in the tm structure are counted from 1900
t.tm_mon = 0; // months are 0-based
t.tm_mday = 2;
t.tm_hour = 3;
t.tm_min = 4;
t.tm_sec = 5;
// timegm interprets tm as UTC (mktime uses local time)
std::printf("%lld\n", (long long)timegm(&t));
return 0;
}
Output
1577934245
C# v11.0.100
v11.0.100
// DateTimeOffset with an explicit UTC offset, equivalent to mktime with the timezone set to UTC
var t = new DateTimeOffset(2020, 1, 2, 3, 4, 5, TimeSpan.Zero);
Console.WriteLine(t.ToUnixTimeSeconds());
Output
1577934245
Go v1.27.4
v1.27.4
package main
import (
"fmt"
"time"
)
func main() {
fmt.Println(time.Date(2020, 1, 2, 3, 4, 5, 0, time.UTC).Unix())
}
Output
1577934245
Java v27.0.1
v27.0.1
import java.time.LocalDateTime;
import java.time.ZoneOffset;
public class Main {
public static void main(String[] args) {
System.out.println(LocalDateTime.of(2020, 1, 2, 3, 4, 5).toEpochSecond(ZoneOffset.UTC));
}
}
Output
1577934245
JavaScript v26.13.1
v26.13.1
console.log(Date.UTC(2020, 0, 2, 3, 4, 5) / 1000)
Output
1577934245
PHP v8.6.0
v8.6.0
date_default_timezone_set("UTC");
echo mktime(3, 4, 5, 1, 2, 2020);
Output
1577934245
Python v3.15.2
v3.15.2
import calendar
print(calendar.timegm((2020, 1, 2, 3, 4, 5)))
Output
1577934245
Rust v1.99.1
v1.99.1
fn main() {
// chrono builds the datetime in UTC and takes the timestamp (argument order same as PHP mktime: hour/min/sec/mon/day/year)
let dt = chrono::NaiveDate::from_ymd_opt(2020, 1, 2)
.unwrap()
.and_hms_opt(3, 4, 5)
.unwrap()
.and_utc();
println!("{}", dt.timestamp());
}
Output
1577934245
TypeScript v26.13.1
v26.13.1
console.log(Date.UTC(2020, 0, 2, 3, 4, 5) / 1000);
Output
1577934245
Language differences
C: fills a struct tm and takes the UTC timestamp with timegm().
Go: time.Date.
Java: LocalDateTime.of.
JavaScript: with Date.UTC, note that months start from 0.
PHP: mktime takes arguments in the order (hour, minute, second, month, day, year).
Python: calendar.timegm uses UTC.
Rust: builds the date-time with the chrono crate and takes the Unix timestamp via timestamp(); the example pins the UTC convention.