I am working on a project, where user can enter a date and time, I calculating something from it, and I want to store it into a database.
Because MySql
can store datetime only from 1000-01-01
then it seems good idea to store it in timestamp.
Now, I want to check, can I create a DateTime
object, get the timestamp, and try to convert back to date.
$UtcTime = \DateTime::createFromFormat("Y-m-d H:i:s", '0-01-01 00:00:00', new \DateTimeZone('UTC'));
var_dump($UtcTime);
var_dump($UtcTime->format('U'));
var_dump(\DateTime::createFromFormat('U', $UtcTime->format('U'), new \DateTimeZone('UTC')));
The output is this:
object(DateTime)[8]
public 'date' => string '0000-01-01 00:00:00.000000' (length=26)
public 'timezone_type' => int 3
public 'timezone' => string 'UTC' (length=3)
string '-62167219200' (length=12)
boolean false
I've tried to use createFromFormat('-U'...
but does not helped. Can sombody tell me, what do I miss when I want to convert it back?
UPDATE
Ok, now I've checked the other topic, and tried this:
$dt = new \DateTime();
$dt->setTimestamp($UtcTime->format('U')); //<--- Pass a UNIX TimeStamp
var_dump($dt->format('Y-m-d H:i:s'));
And get:1905-06-06 19:35:44
what is not 0000-01-01 00:00:00
UPDATE2
I tried to play with this, and I checkd it with closer dates to now.
1782-01-01 00:00:00
become 1918-02-08 07:28:16
with TS -5932656000
.
So I've checked the manual:
Prior to PHP 5.1.0, not all platforms support negative timestamps, therefore your date range may be limited to no earlier than the Unix epoch. This means that e.g. dates prior to Jan 1, 1970 will not work on Windows, some Linux distributions, and a few other operating systems.
I am working on localhost with this:
- Windows 10 Enterprise N 64bit
- Apache 2.4.4 64bit
- PHP 5.6.5 64bit
When I've upload it to our linux box, I've got the proper datetime.