0

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.

deceze
  • 510,633
  • 85
  • 743
  • 889
vaso123
  • 12,347
  • 4
  • 34
  • 64
  • You want to use dates before the year 1000? You'll probably find very bad or wonky native support for this all around, since dates weren't exactly nailed down very precisely back then. – deceze Feb 04 '16 at 10:43
  • seconds are just seconds even BC. This is an astrology project, and what if I want to know something about Homerus? – vaso123 Feb 04 '16 at 10:48
  • 1
    what is wrong with the native `strtotime`? – Kevin Kopf Feb 04 '16 at 10:52
  • Possible duplicate of [Convert from MySQL datetime to another format with PHP](http://stackoverflow.com/questions/136782/convert-from-mysql-datetime-to-another-format-with-php) – tony gil Feb 04 '16 at 10:54
  • @Nordenheim that can usable only from 1970... – vaso123 Feb 04 '16 at 10:55
  • 1
    Possible duplicate of [PHP: Convert negative timestamp to date](http://stackoverflow.com/questions/21522936/php-convert-negative-timestamp-to-date) – tomas.lang Feb 04 '16 at 10:55
  • Yes, you can pretty precisely say *"this event was 600 billion seconds ago"*, or whatever. You'll have a much harder time saying *"...and the date back then was May 12th in year 34 at 12:45pm"*, because back then people may have had differing opinions about that in neighbouring cities... :) – deceze Feb 04 '16 at 10:56
  • this question is a fundamentally a duplicate and i was the one to flag it as such (i hate flag-and-run, the least i can do is explain why i did what i did). the answer abounds in SO and can easily be found on google. it could just as easily have been voted to be closed for lack of effort in researching the answer. sorry m8. – tony gil Feb 04 '16 at 10:57
  • @tonygil Updated my post. – vaso123 Feb 04 '16 at 11:04
  • BTW, there is no *year 0*. 1 BC is followed by 1 AD. – deceze Feb 04 '16 at 11:17
  • Ok, but it does not matter. If I try it with: `1712-01-01 00:00:00.00000` TS will `-8141731200` and the new DateTime is `1984-03-15 13:56:32` – vaso123 Feb 04 '16 at 11:35
  • 1
    It could be something with PHP version or configuration. It works for me. – Michas Feb 04 '16 at 11:49
  • @Michas Yes, it is. I am working on localhost with Win10 64bit, Apache 2.4 64bit, and PHP 5.6 64bit. Now I uploaded it to a liux box, and it works, I will update my post soon. – vaso123 Feb 04 '16 at 11:54

1 Answers1

-1

I believe this is what you're after.

$date1 = new DateTime();

$timestamp = $date1->getTimestamp();

sleep(2);

$date2 = new DateTime();
$date3 = new DateTime();

$date2->setTimestamp($timestamp);

var_dump($date1 == $date2); // true
var_dump($date1 == $date3); // false
Tom Wright
  • 2,841
  • 4
  • 22
  • 30