Converting a UNIX Timestamp to Formatted Date String
Try gmdate
like this:
<?php
$timestamp=1333699439;
echo gmdate("Y-m-d\TH:i:s\Z", $timestamp);
?>
Convert a Unix timestamp to time in JavaScript
let unix_timestamp = 1549312452// Create a new JavaScript Date object based on the timestamp// multiplied by 1000 so that the argument is in milliseconds, not seconds.var date = new Date(unix_timestamp * 1000);// Hours part from the timestampvar hours = date.getHours();// Minutes part from the timestampvar minutes = "0" + date.getMinutes();// Seconds part from the timestampvar seconds = "0" + date.getSeconds();
// Will display time in 10:30:23 formatvar formattedTime = hours + ':' + minutes.substr(-2) + ':' + seconds.substr(-2);
console.log(formattedTime);
Convert Unix timestamp to a date string
With date
from GNU coreutils you can do:
date -d "@$TIMESTAMP"
# date -d @0
Wed Dec 31 19:00:00 EST 1969
(From: BASH: Convert Unix Timestamp to a Date)
On OS X, use date -r
.
date -r "$TIMESTAMP"
Alternatively, use strftime()
. It's not available directly from the shell, but you can access it via gawk. The %c
specifier displays the timestamp in a locale-dependent manner.
echo "$TIMESTAMP" | gawk '{print strftime("%c", $0)}'
# echo 0 | gawk '{print strftime("%c", $0)}'
Wed 31 Dec 1969 07:00:00 PM EST
How to convert a Unix timestamp to 2000-01-01 or 2000-05-24 20:00:00 format or reverse in Deno?
Aside from the standard Javascript ways, there is a date/time library for Deno called Ptera, that can be used as follows:
import { datetime } from "https://deno.land/x/ptera/mod.ts";
const dt = datetime("2000-05-24 20:00:00");
console.log(dt.format("X")); // X for Unix timestamp in seconds 959198400
console.log(dt.format("x")); // x for "Unix timestamp" in milliseconds 959198400000
const dt2 = datetime(1646245390158);
console.log(dt2.format("YYYY-MM-dd HH:mm:ss")); // output: 2022-03-02 19:23:10
A UNIX timestamp is the number of seconds since 1970-01-01 00:00:00 UTC, the Javascript timestamp is in milliseconds and sometimes in documentations, they also call this as a UNIX timestamp or UNIX Epoch time.
A detailed reference about the formatting options is available here.
Unable to convert unix timestamp in date format
You should try this:
import requests, json
import datetime as dt
r = requests.get('https://api1.binance.com/api/v3/ticker/24hr')
jm = json.loads(r.text)
for n in range(len(jm)):
x=(jm[n] ['closeTime'])
# printing unix timestamp
print (x)
# trying to get the date time
time_val = dt.datetime.fromtimestamp(x/1000).strftime('%Y-%m-%d %H:%M:%S')
print(time_val)
Last Few Output:
1630932847657
2021-09-06 18:54:07
1630932864523
2021-09-06 18:54:24
1630932804755
2021-09-06 18:53:24
1630932871298
2021-09-06 18:54:31
1630932771147
2021-09-06 18:52:51
1630932862321
2021-09-06 18:54:22
1630932863872
2021-09-06 18:54:23
1630932871522
2021-09-06 18:54:31
1630932870246
2021-09-06 18:54:30
1630932872709
2021-09-06 18:54:32
1630932872684
2021-09-06 18:54:32
1630932870210
2021-09-06 18:54:30
1630932872655
2021-09-06 18:54:32
1630932800127
2021-09-06 18:53:20
Related Topics
How to Add a PHP Page to Wordpress
Convert/Cast an Stdclass Object to Another Class
MySQLi Count(*) Always Returns 1
Who Should Handle the Conditions in Complex Queries, the Data Mapper or the Service Layer
When to Use Static VS Instantiated Classes
PHP: Convert Unicode Codepoint to Utf-8
PHP Json_Encode Not Working on Arrays Partially
PHP: How to Check If Image File Exists
Fastest Way to Serve a File Using PHP
PHP Pdo: Get the Columns Name of a Table
How to Prevent My Site Page to Be Loaded Via 3Rd Party Site Frame of Iframe
Why Is PHP Session_Destroy() Not Working
: Operator (The 'Elvis Operator') in PHP
In PHP (≫= 5.0), Is Passing by Reference Faster