strtotime
strtotime(string $datetime, ?int $baseTimestamp = null): int|false
Parses a date/time string into a Unix timestamp.
C C23
C23
#include <stdio.h>
#include <time.h>
int main(void) {
struct tm t;
// strptime parses by format string (POSIX; needs a custom implementation on Windows)
strptime("2020-01-02 03:04:05", "%Y-%m-%d %H:%M:%S", &t);
printf("%lld\n", (long long)timegm(&t)); // take the timestamp as UTC
// no relative-format parser: add 86400 seconds by hand (what "+1 day" means)
struct tm d2;
strptime("2020-01-02 03:04:05", "%Y-%m-%d %H:%M:%S", &d2);
printf("%lld\n", (long long)timegm(&d2) + 86400);
return 0;
}
Output
1577934245 1578020645
C++ C++26
C++26
#include <cstdio>
#include <ctime>
int main() {
std::tm t = {};
// strptime parses the datetime by format string (POSIX)
strptime("2020-01-02 03:04:05", "%Y-%m-%d %H:%M:%S", &t);
std::printf("%lld\n", (long long)timegm(&t)); // take the timestamp as UTC
// no relative-format parser: add 86400 seconds by hand
std::tm d2 = {};
strptime("2020-01-02 03:04:05", "%Y-%m-%d %H:%M:%S", &d2);
std::printf("%lld\n", (long long)timegm(&d2) + 86400);
return 0;
}
Output
1577934245 1578020645
C# v11.0.100
v11.0.100
var t = DateTimeOffset.Parse("2020-01-02 03:04:05",
System.Globalization.CultureInfo.InvariantCulture,
System.Globalization.DateTimeStyles.AssumeUniversal);
Console.WriteLine(t.ToUnixTimeSeconds());
// AddDays is the C# route to "+1 day"
var t2 = DateTimeOffset.Parse("2020-01-02 03:04:05",
System.Globalization.CultureInfo.InvariantCulture,
System.Globalization.DateTimeStyles.AssumeUniversal).AddDays(1);
Console.WriteLine(t2.ToUnixTimeSeconds());
Output
1577934245 1578020645
Go v1.27.4
v1.27.4
package main
import (
"fmt"
"time"
)
func main() {
t, _ := time.Parse("2006-01-02 15:04:05", "2020-01-02 03:04:05")
fmt.Println(t.Unix())
// AddDate is the Go route to "+1 day"
t2, _ := time.Parse("2006-01-02 15:04:05", "2020-01-02 03:04:05")
fmt.Println(t2.AddDate(0, 0, 1).Unix())
}
Output
1577934245 1578020645
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.parse("2020-01-02T03:04:05").toEpochSecond(ZoneOffset.UTC));
// plusDays is the Java route to "+1 day"
System.out.println(LocalDateTime.parse("2020-01-02T03:04:05").plusDays(1).toEpochSecond(ZoneOffset.UTC));
}
}
Output
1577934245 1578020645
JavaScript v26.13.1
v26.13.1
console.log(Date.parse("2020-01-02T03:04:05Z") / 1000)
// Date arithmetic is manual: add 86400 seconds
console.log((Date.parse("2020-01-02T03:04:05Z") + 86400000) / 1000)
Output
1577934245 1578020645
PHP v8.6.0
v8.6.0
date_default_timezone_set("UTC");
echo strtotime("2020-01-02 03:04:05 UTC"), "\n";
// relative formats are strtotime's superpower: "+1 day", "next monday"...
date_default_timezone_set("UTC");
echo strtotime("2020-01-02 03:04:05 UTC +1 day"), "
";
echo strtotime("+1 day", 1577934245); // second arg = base timestamp
Output
1577934245 1578020645 1578020645
Python v3.15.2
v3.15.2
import datetime
print(int(datetime.datetime.strptime("2020-01-02 03:04:05", "%Y-%m-%d %H:%M:%S").replace(tzinfo=datetime.timezone.utc).timestamp()))
# timedelta is the Python route to "+1 day"
print(int((datetime.datetime.strptime("2020-01-02 03:04:05", "%Y-%m-%d %H:%M:%S")
.replace(tzinfo=datetime.timezone.utc) + datetime.timedelta(days=1)).timestamp()))
Output
1577934245 1578020645
Rust v1.99.1
v1.99.1
fn main() {
// chrono parses by format string (the explicit-format equivalent of PHP strtotime), then takes the UTC timestamp
let dt = chrono::NaiveDateTime::parse_from_str("2020-01-02 03:04:05", "%Y-%m-%d %H:%M:%S")
.unwrap()
.and_utc();
println!("{}", dt.timestamp());
// chrono: + Duration::days is the route to "+1 day"
let dt2 = chrono::NaiveDateTime::parse_from_str("2020-01-02 03:04:05", "%Y-%m-%d %H:%M:%S")
.unwrap()
.and_utc()
+ chrono::Duration::days(1);
println!("{}", dt2.timestamp());
}
Output
1577934245 1578020645
TypeScript v26.13.1
v26.13.1
console.log(Date.parse("2020-01-02T03:04:05Z") / 1000);
console.log((Date.parse("2020-01-02T03:04:05Z") + 86400000) / 1000); // +1 day in ms
Output
1577934245 1578020645
Language differences
C: parses with POSIX strptime() and takes the timestamp via timegm().
Go: uses layout strings with time.Parse.
Java: uses LocalDateTime.parse (ISO by default).
JavaScript: Date.parse works best with ISO 8601.
PHP: strtotime understands a wide range of natural-language formats.
Python: needs strptime with an explicit format.
Rust: parses with chrono::parse_from_str using an explicit format string (PHP strtotime natural-language formats have no direct equivalent), then takes the UTC timestamp.