1532131481886863
I tried the code below and don't work it gives me wrong date , i think has something to do with the amount of digits
$epoch = 1532131481886863;
$dt = new DateTime("#$epoch"); // convert UNIX timestamp to PHP DateTime
echo $dt->format('Y-m-d H:i:s');
The only thing I can think of is that the last six digits is microseconds.
So splitting the number will give you correct date and time.
echo date("Y-m-d H:i:s", substr($epoch, 0,-6));
// Optionally you can echo the microseconds also.
echo " " . substr($epoch, -6);
https://3v4l.org/QDQnc
<?php
// Divide by 1000*1000 because given number is epoch with microseconds
// but the DateTime expects time in seconds
// By dividing we will get 1532131481.886863
// We dont need the microseconds so we cast (int) on it to get 1532131481
$epoch = (int)((1532131481886863 /1000) /1000);
$dt = new DateTime("#$epoch"); // convert UNIX timestamp to PHP DateTime
echo $dt->format('Y-m-d H:i:s');
Related
I have this 13 digit timestamp 1443852054000 that i want to convert to date and time but dont succeed. I have tried this codes:
echo date('Y-m-d h:i:s',$item->timestamp);
doesnt work for me and also this
$unix_time = date('Ymdhis', strtotime($datetime ));
and this :
$item = strtotime($txn_row['appoint_date']);
<?php echo date("Y-m-d H:i:s", $time); ?>
what should i use?
This timestamp is in milliseconds, not in seconds. Divide it by 1000 and use date function:
echo date('Y-m-d h:i:s', $item->timestamp / 1000);
// e.g
echo date('Y-m-d h:i:s',1443852054000/1000);
// shows 2015-10-03 02:00:54
A 13 digit timestamp is used in JavaScript to represent time in milliseconds. In PHP 10 a digit timestamp is used to represent time in seconds. So divide by 1000 and round off to get 10 digits.
$timestamp = 1443852054000;
echo date('Y-m-d h:i:s', floor($timestamp / 1000));
You can achieve this with DateTime::createFromFormat.
Because you've a timestamp with 13 digits, you'll have to divide it by 1000, in order to use it with DateTime, i.e.:
$ts = 1443852054000 / 1000; // we're basically removing the last 3 zeros
$date = DateTime::createFromFormat("U", $ts)->format("Y-m-d h:i:s");
echo $date;
//2015-10-03 06:00:54
DEMO
http://sandbox.onlinephpfunctions.com/code/d0d01718e0fc02574b401e798aaa201137658acb
You may want to set the default timezone to avoid any warnings
date_default_timezone_set('Europe/Lisbon');
NOTE:
More about php date and time at php the right way
Could someone assist me on a timestamp issue.. how can I subtract 2 minutes from this timestamp?
echo 'Settings from database (octopus_import_employees):';
$settings = get_settings('octopus_import_employees');
var_dump($settings);
echo 'Timestamp in human format (Started timestamp):';
$started = date("Y-m-d H:i:s", $settings['started']);
var_dump($started);
var_dump($settings); gets an unix timestamp such as: 342534534
var_dump($started); converts it to a readable format such as: 2019-11-08 05:08:58.
All help would be appreciated.
Timestamps are in seconds so you can subtract 120 seconds (i.e. 2 minutes) from it
$time = $settings['started'] - 120;
echo 'Timestamp in human format (Started timestamp):';
$started = date("Y-m-d H:i:s", $time);
var_dump($started);
#YasinPatel solution is definitely the simplest for your situation. In situations where you don't have a unix timestamp input, one of these methods might be easier to use.
You could create a DateTime object from your timestamp using date_create_from_format and subtract 2 minutes from it, using either sub or modify:
$started = date_create_from_format('U', $settings['started']);
$started->sub(new DateInterval('PT2M'));
echo $started->format('Y-m-d H:i:s');
or
$started = date_create_from_format('U', $settings['started']);
$started->modify('-2 minutes');
echo $started->format('Y-m-d H:i:s');
Demo on 3v4l.org
I would like to convert the timestamps from ten minutes ago and now to match the following format:
2018-09-23T04:47:07.237
Here are the timestamps I'd like to convert to match the above format:
$now = date('m/d/y g:i a');
$now = strtotime($now);
$ten_minutes_ago = strtotime('-10 minutes');
How can I do this? Thanks!
Use date_format function instead. You don't need to convert to UNIX timestamp using strtotime function. Instead use DateTime library
Check the following (Rextester Demo):
$now = new DateTime(); // create a datetime object
$sub = new DateInterval("PT10M"); // Interval of 10 mins
$ten_minutes_ago = new DateTime();
$ten_minutes_ago->sub($sub); // subtract 10 minutes
// changed formats
$now_changed = date_format($now, DATE_ISO8601);
$ten_minutes_ago_changed = date_format($ten_minutes_ago, DATE_ISO8601);
// print output
echo $now_changed; //2018-09-23T02:58:25-0400
echo $ten_minutes_ago_changed; // 2018-09-23T02:48:25-0400
Details:
The date_format() function returns a date formatted according to the specified format.
DATE_ISO8601 - ISO-8601 (example: 2013-04-12T15:52:01+0000)
You can check for more formatting options here.
Here is how I would do what you are asking for.
If your data is in a string. Here is the only line of code you need:
$date = date('m/d/y g:i a'); //Gets a date string.
echo substr(date('Y-m-d\TH:i:s.u', strtotime($date . ' -10 minutes')), 0, -3); // PHP < 7.0.0
//echo date('Y-m-d\TH:i:s.v', strtotime($date . ' -10 minutes')); //PHP > 7.0.0
This will produce:
Ex.
09/23/18 12:13 am
To
2018-09-23T00:03:00.000
One thing to note here. The microseconds will always be zeros if your original input date is a string and in the format m/d/y g:i a that you have specified. The reason being is that there is no millisecond information to be had from the date string.
If you create you input date as a dateTime object, the object will be able to keep track of the microseconds for you.
I have tried to solve it by extracting the numeric part and then parsed it using date function. But it shows me some old date which I guess is not correct.
$datef = "1490914800000+0100";
$adada = date('Y-m-d H:i:s', $datef);
// Gives date 1987-10-13 18:31:28 which is an old date. Please suggest.
One approach, well-covered by this SO question, is to use the DateTime() function to convert time in seconds since epoch to a date, and then display this date using format(). But there are two caveats with your data. First, you appear to have milliseconds since the epoch, which needs to be converted to seconds. Second, you also have a timezone shift, in hours, tagged to the end. I split your $datef string into two parts, epoch and timezone, then arrive at the number of seconds since epoch.
list($epoch, $timezone) = explode('+', $datef);
$epoch = ($epoch / 1000) + (substr($timezone, 0, 2)*60*60) +
(substr($timezone, 2, 2)*60);
$dt = new DateTime("#$epoch");
echo $dt->format('Y-m-d H:i:s');
Output:
2017-03-31 00:00:00
Demo here:
PHP Sandbox
The time seems to be in milliseconds.
You can add the timezone shift to the seconds. 1 hour = 3600 seconds.
$milliSeconds = intval("1490914800000");
$seconds = $milliSeconds/1000;
$date = date("Y-m-d H:i:s", $seconds);
i've got 2 time stamps: $start_time = 1312346227; and $end_time = 1312346466;
and i am trying to substract them to see the time inbetween $end_time = $end_time - $start_time;
and i get 239.
What is the proper way of converting this to a human readable date?
if i try echo date("h:i:s A",$end_time); i get 04:03:59 and it should be 00:03:59
any ideas?
Thanks
If you have PHP 5.3, use the DateInterval class.
Example stolen from the manual page on DateInterval::format():
<?php
$january = new DateTime('2010-01-01');
$february = new DateTime('2010-02-01');
$interval = $february->diff($january);
// %a will output the total number of days.
echo $interval->format('%a total days')."\n";
// While %d will only output the number of days not already covered by the
// month.
echo $interval->format('%m month, %d days');
?>
You get addiionl four hours, because of your timezone. Remember that unix timestamp is the number of seconds since 1970-01-01 00:00:00 UTC. If you (or your server) are in UTC+4 TZ, then date() will implicitly do a timezone conversion to your local time.
Solution? Use gmdate() instead
You need to set your timezone correctly. http://www.php.net/manual/en/function.date-default-timezone-set.php