Convert timestamp to datetime instance












-1















I am having the following PHP setup:



$ php --version
PHP 7.1.8 (cli) (built: Aug 1 2017 21:10:46) ( ZTS MSVC14 (Visual C++ 2015) x86
)
Copyright (c) 1997-2017 The PHP Group
Zend Engine v3.1.0, Copyright (c) 1998-2017 Zend Technologies
with Xdebug v2.6.1, Copyright (c) 2002-2018, by Derick Rethans


I am using xampp, so I am running a 32-bit version on my 64-bit windows 8.1 machine.



I would like to convert the following timestamp into a Datetime instance:



$timestamp = 1546438627830;
$t = DateTime::createFromFormat('U',$timestamp);
echo $t;


However, I get the following exception:



Catchable fatal error: Object of class DateTime could not be converted to string


I was thinking that my 32-bit PHP version might cause some issues. Any suggestions what I am doing wrong?



Thx in advance for your reply!



UPDATE



After using var_dump() I get the following:



$timestamp = 1546438627830;
$t = DateTime::createFromFormat('U',$timestamp);
var_dump($t);


Output:



class DateTime#1 (3) {
public $date =>
string(20) "50974-09-09 05:30:30"
public $timezone_type =>
int(1)
public $timezone =>
string(6) "+00:00"
}


As you can see the wrong timestamp is converted?



Any suggestions why?



UPDATE



After trying several things I found the following solution:



$timestamp = 1546438627830;
$result = date ('c', (int) round ($timestamp / 1000));
$msec = (int) $timestamp % 1000;
$t = strtotime(str_replace ('+00:00', sprintf (".%03dZ", $msec), $result));

$finalTime = DateTime::createFromFormat('U', $t);

var_dump($finalTime);









share|improve this question

























  • Why do you think it is wrong?

    – Alex
    Jan 2 at 14:45











  • @Alex I printed the var_dump output, however I still get the wrong value for the timestamp. Any suggestions why?

    – Anna.Klee
    Jan 2 at 14:46











  • year value equal to 50974 technically it is not wrong, it is just weird and you just don't expect it. Once you can explain why you expect different result - you will be able to answer your question yourself next time and will realize why it is obvious for some people.

    – Alex
    Jan 2 at 14:53


















-1















I am having the following PHP setup:



$ php --version
PHP 7.1.8 (cli) (built: Aug 1 2017 21:10:46) ( ZTS MSVC14 (Visual C++ 2015) x86
)
Copyright (c) 1997-2017 The PHP Group
Zend Engine v3.1.0, Copyright (c) 1998-2017 Zend Technologies
with Xdebug v2.6.1, Copyright (c) 2002-2018, by Derick Rethans


I am using xampp, so I am running a 32-bit version on my 64-bit windows 8.1 machine.



I would like to convert the following timestamp into a Datetime instance:



$timestamp = 1546438627830;
$t = DateTime::createFromFormat('U',$timestamp);
echo $t;


However, I get the following exception:



Catchable fatal error: Object of class DateTime could not be converted to string


I was thinking that my 32-bit PHP version might cause some issues. Any suggestions what I am doing wrong?



Thx in advance for your reply!



UPDATE



After using var_dump() I get the following:



$timestamp = 1546438627830;
$t = DateTime::createFromFormat('U',$timestamp);
var_dump($t);


Output:



class DateTime#1 (3) {
public $date =>
string(20) "50974-09-09 05:30:30"
public $timezone_type =>
int(1)
public $timezone =>
string(6) "+00:00"
}


As you can see the wrong timestamp is converted?



Any suggestions why?



UPDATE



After trying several things I found the following solution:



$timestamp = 1546438627830;
$result = date ('c', (int) round ($timestamp / 1000));
$msec = (int) $timestamp % 1000;
$t = strtotime(str_replace ('+00:00', sprintf (".%03dZ", $msec), $result));

$finalTime = DateTime::createFromFormat('U', $t);

var_dump($finalTime);









share|improve this question

























  • Why do you think it is wrong?

    – Alex
    Jan 2 at 14:45











  • @Alex I printed the var_dump output, however I still get the wrong value for the timestamp. Any suggestions why?

    – Anna.Klee
    Jan 2 at 14:46











  • year value equal to 50974 technically it is not wrong, it is just weird and you just don't expect it. Once you can explain why you expect different result - you will be able to answer your question yourself next time and will realize why it is obvious for some people.

    – Alex
    Jan 2 at 14:53
















-1












-1








-1


1






I am having the following PHP setup:



$ php --version
PHP 7.1.8 (cli) (built: Aug 1 2017 21:10:46) ( ZTS MSVC14 (Visual C++ 2015) x86
)
Copyright (c) 1997-2017 The PHP Group
Zend Engine v3.1.0, Copyright (c) 1998-2017 Zend Technologies
with Xdebug v2.6.1, Copyright (c) 2002-2018, by Derick Rethans


I am using xampp, so I am running a 32-bit version on my 64-bit windows 8.1 machine.



I would like to convert the following timestamp into a Datetime instance:



$timestamp = 1546438627830;
$t = DateTime::createFromFormat('U',$timestamp);
echo $t;


However, I get the following exception:



Catchable fatal error: Object of class DateTime could not be converted to string


I was thinking that my 32-bit PHP version might cause some issues. Any suggestions what I am doing wrong?



Thx in advance for your reply!



UPDATE



After using var_dump() I get the following:



$timestamp = 1546438627830;
$t = DateTime::createFromFormat('U',$timestamp);
var_dump($t);


Output:



class DateTime#1 (3) {
public $date =>
string(20) "50974-09-09 05:30:30"
public $timezone_type =>
int(1)
public $timezone =>
string(6) "+00:00"
}


As you can see the wrong timestamp is converted?



Any suggestions why?



UPDATE



After trying several things I found the following solution:



$timestamp = 1546438627830;
$result = date ('c', (int) round ($timestamp / 1000));
$msec = (int) $timestamp % 1000;
$t = strtotime(str_replace ('+00:00', sprintf (".%03dZ", $msec), $result));

$finalTime = DateTime::createFromFormat('U', $t);

var_dump($finalTime);









share|improve this question
















I am having the following PHP setup:



$ php --version
PHP 7.1.8 (cli) (built: Aug 1 2017 21:10:46) ( ZTS MSVC14 (Visual C++ 2015) x86
)
Copyright (c) 1997-2017 The PHP Group
Zend Engine v3.1.0, Copyright (c) 1998-2017 Zend Technologies
with Xdebug v2.6.1, Copyright (c) 2002-2018, by Derick Rethans


I am using xampp, so I am running a 32-bit version on my 64-bit windows 8.1 machine.



I would like to convert the following timestamp into a Datetime instance:



$timestamp = 1546438627830;
$t = DateTime::createFromFormat('U',$timestamp);
echo $t;


However, I get the following exception:



Catchable fatal error: Object of class DateTime could not be converted to string


I was thinking that my 32-bit PHP version might cause some issues. Any suggestions what I am doing wrong?



Thx in advance for your reply!



UPDATE



After using var_dump() I get the following:



$timestamp = 1546438627830;
$t = DateTime::createFromFormat('U',$timestamp);
var_dump($t);


Output:



class DateTime#1 (3) {
public $date =>
string(20) "50974-09-09 05:30:30"
public $timezone_type =>
int(1)
public $timezone =>
string(6) "+00:00"
}


As you can see the wrong timestamp is converted?



Any suggestions why?



UPDATE



After trying several things I found the following solution:



$timestamp = 1546438627830;
$result = date ('c', (int) round ($timestamp / 1000));
$msec = (int) $timestamp % 1000;
$t = strtotime(str_replace ('+00:00', sprintf (".%03dZ", $msec), $result));

$finalTime = DateTime::createFromFormat('U', $t);

var_dump($finalTime);






php datetime






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 2 at 14:54







Anna.Klee

















asked Jan 2 at 14:36









Anna.KleeAnna.Klee

1,2531165135




1,2531165135













  • Why do you think it is wrong?

    – Alex
    Jan 2 at 14:45











  • @Alex I printed the var_dump output, however I still get the wrong value for the timestamp. Any suggestions why?

    – Anna.Klee
    Jan 2 at 14:46











  • year value equal to 50974 technically it is not wrong, it is just weird and you just don't expect it. Once you can explain why you expect different result - you will be able to answer your question yourself next time and will realize why it is obvious for some people.

    – Alex
    Jan 2 at 14:53





















  • Why do you think it is wrong?

    – Alex
    Jan 2 at 14:45











  • @Alex I printed the var_dump output, however I still get the wrong value for the timestamp. Any suggestions why?

    – Anna.Klee
    Jan 2 at 14:46











  • year value equal to 50974 technically it is not wrong, it is just weird and you just don't expect it. Once you can explain why you expect different result - you will be able to answer your question yourself next time and will realize why it is obvious for some people.

    – Alex
    Jan 2 at 14:53



















Why do you think it is wrong?

– Alex
Jan 2 at 14:45





Why do you think it is wrong?

– Alex
Jan 2 at 14:45













@Alex I printed the var_dump output, however I still get the wrong value for the timestamp. Any suggestions why?

– Anna.Klee
Jan 2 at 14:46





@Alex I printed the var_dump output, however I still get the wrong value for the timestamp. Any suggestions why?

– Anna.Klee
Jan 2 at 14:46













year value equal to 50974 technically it is not wrong, it is just weird and you just don't expect it. Once you can explain why you expect different result - you will be able to answer your question yourself next time and will realize why it is obvious for some people.

– Alex
Jan 2 at 14:53







year value equal to 50974 technically it is not wrong, it is just weird and you just don't expect it. Once you can explain why you expect different result - you will be able to answer your question yourself next time and will realize why it is obvious for some people.

– Alex
Jan 2 at 14:53














1 Answer
1






active

oldest

votes


















2














When using echo on an object, PHP tries to call the magic __toString() method on that object. If it can't do that, it will throw the error you get. I recommend using var_dump() to debug objects.



https://3v4l.org/GlJaW



<?php

$timestamp = 1546438627830;
$t = DateTime::createFromFormat('U',$timestamp);
var_dump($t);


References:



http://php.net/manual/en/function.var-dump.php



http://php.net/manual/en/function.echo.php



http://php.net/manual/en/language.oop5.magic.php



http://php.net/manual/de/datetime.createfromformat.php



EDIT: Handling milliseconds



<?php

$timestamp = (int)(1546438627830 / 1000);
$t = DateTime::createFromFormat('U',$timestamp);
var_dump($t);





share|improve this answer


























  • Thx for your reply! I used var_dump to "debug" the timestamp. However, still I get the wrong output? I kindly ask for your opinion what might be the reason for this? How to correctly parse it?

    – Anna.Klee
    Jan 2 at 14:45











  • @Anna.Klee Well your timestamp is in milliseconds. Try the edit. Make sure to read the documentation page to DateTime::createFromFormat. The U modifier means seconds since the unix epoch, not milliseconds. Thus the weird behavior.

    – Xatenev
    Jan 2 at 14:50













Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54008218%2fconvert-timestamp-to-datetime-instance%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









2














When using echo on an object, PHP tries to call the magic __toString() method on that object. If it can't do that, it will throw the error you get. I recommend using var_dump() to debug objects.



https://3v4l.org/GlJaW



<?php

$timestamp = 1546438627830;
$t = DateTime::createFromFormat('U',$timestamp);
var_dump($t);


References:



http://php.net/manual/en/function.var-dump.php



http://php.net/manual/en/function.echo.php



http://php.net/manual/en/language.oop5.magic.php



http://php.net/manual/de/datetime.createfromformat.php



EDIT: Handling milliseconds



<?php

$timestamp = (int)(1546438627830 / 1000);
$t = DateTime::createFromFormat('U',$timestamp);
var_dump($t);





share|improve this answer


























  • Thx for your reply! I used var_dump to "debug" the timestamp. However, still I get the wrong output? I kindly ask for your opinion what might be the reason for this? How to correctly parse it?

    – Anna.Klee
    Jan 2 at 14:45











  • @Anna.Klee Well your timestamp is in milliseconds. Try the edit. Make sure to read the documentation page to DateTime::createFromFormat. The U modifier means seconds since the unix epoch, not milliseconds. Thus the weird behavior.

    – Xatenev
    Jan 2 at 14:50


















2














When using echo on an object, PHP tries to call the magic __toString() method on that object. If it can't do that, it will throw the error you get. I recommend using var_dump() to debug objects.



https://3v4l.org/GlJaW



<?php

$timestamp = 1546438627830;
$t = DateTime::createFromFormat('U',$timestamp);
var_dump($t);


References:



http://php.net/manual/en/function.var-dump.php



http://php.net/manual/en/function.echo.php



http://php.net/manual/en/language.oop5.magic.php



http://php.net/manual/de/datetime.createfromformat.php



EDIT: Handling milliseconds



<?php

$timestamp = (int)(1546438627830 / 1000);
$t = DateTime::createFromFormat('U',$timestamp);
var_dump($t);





share|improve this answer


























  • Thx for your reply! I used var_dump to "debug" the timestamp. However, still I get the wrong output? I kindly ask for your opinion what might be the reason for this? How to correctly parse it?

    – Anna.Klee
    Jan 2 at 14:45











  • @Anna.Klee Well your timestamp is in milliseconds. Try the edit. Make sure to read the documentation page to DateTime::createFromFormat. The U modifier means seconds since the unix epoch, not milliseconds. Thus the weird behavior.

    – Xatenev
    Jan 2 at 14:50
















2












2








2







When using echo on an object, PHP tries to call the magic __toString() method on that object. If it can't do that, it will throw the error you get. I recommend using var_dump() to debug objects.



https://3v4l.org/GlJaW



<?php

$timestamp = 1546438627830;
$t = DateTime::createFromFormat('U',$timestamp);
var_dump($t);


References:



http://php.net/manual/en/function.var-dump.php



http://php.net/manual/en/function.echo.php



http://php.net/manual/en/language.oop5.magic.php



http://php.net/manual/de/datetime.createfromformat.php



EDIT: Handling milliseconds



<?php

$timestamp = (int)(1546438627830 / 1000);
$t = DateTime::createFromFormat('U',$timestamp);
var_dump($t);





share|improve this answer















When using echo on an object, PHP tries to call the magic __toString() method on that object. If it can't do that, it will throw the error you get. I recommend using var_dump() to debug objects.



https://3v4l.org/GlJaW



<?php

$timestamp = 1546438627830;
$t = DateTime::createFromFormat('U',$timestamp);
var_dump($t);


References:



http://php.net/manual/en/function.var-dump.php



http://php.net/manual/en/function.echo.php



http://php.net/manual/en/language.oop5.magic.php



http://php.net/manual/de/datetime.createfromformat.php



EDIT: Handling milliseconds



<?php

$timestamp = (int)(1546438627830 / 1000);
$t = DateTime::createFromFormat('U',$timestamp);
var_dump($t);






share|improve this answer














share|improve this answer



share|improve this answer








edited Jan 2 at 14:50

























answered Jan 2 at 14:42









XatenevXatenev

5,76921235




5,76921235













  • Thx for your reply! I used var_dump to "debug" the timestamp. However, still I get the wrong output? I kindly ask for your opinion what might be the reason for this? How to correctly parse it?

    – Anna.Klee
    Jan 2 at 14:45











  • @Anna.Klee Well your timestamp is in milliseconds. Try the edit. Make sure to read the documentation page to DateTime::createFromFormat. The U modifier means seconds since the unix epoch, not milliseconds. Thus the weird behavior.

    – Xatenev
    Jan 2 at 14:50





















  • Thx for your reply! I used var_dump to "debug" the timestamp. However, still I get the wrong output? I kindly ask for your opinion what might be the reason for this? How to correctly parse it?

    – Anna.Klee
    Jan 2 at 14:45











  • @Anna.Klee Well your timestamp is in milliseconds. Try the edit. Make sure to read the documentation page to DateTime::createFromFormat. The U modifier means seconds since the unix epoch, not milliseconds. Thus the weird behavior.

    – Xatenev
    Jan 2 at 14:50



















Thx for your reply! I used var_dump to "debug" the timestamp. However, still I get the wrong output? I kindly ask for your opinion what might be the reason for this? How to correctly parse it?

– Anna.Klee
Jan 2 at 14:45





Thx for your reply! I used var_dump to "debug" the timestamp. However, still I get the wrong output? I kindly ask for your opinion what might be the reason for this? How to correctly parse it?

– Anna.Klee
Jan 2 at 14:45













@Anna.Klee Well your timestamp is in milliseconds. Try the edit. Make sure to read the documentation page to DateTime::createFromFormat. The U modifier means seconds since the unix epoch, not milliseconds. Thus the weird behavior.

– Xatenev
Jan 2 at 14:50







@Anna.Klee Well your timestamp is in milliseconds. Try the edit. Make sure to read the documentation page to DateTime::createFromFormat. The U modifier means seconds since the unix epoch, not milliseconds. Thus the weird behavior.

– Xatenev
Jan 2 at 14:50






















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54008218%2fconvert-timestamp-to-datetime-instance%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Mossoró

Error while reading .h5 file using the rhdf5 package in R

Pushsharp Apns notification error: 'InvalidToken'