Jack M.
Jack M.

Reputation: 3804

PHP difference between time() and getTimestamp() object [Year 2038 bug]

What is the difference between:

$now = time();

and

$now = new DateTime();
$now->getTimestamp();

By taking into account the 32-Bit INT limitations (a.k.a year 2038 bug) is it safe to use getTimestamp() in a 32-Bit system ?

Edit:

For further information about this problem, check this link: What is a Unix timestamp and why use it?

Upvotes: 1

Views: 2348

Answers (2)

Torsten Römer
Torsten Römer

Reputation: 3926

Since DateTime::getTimestamp() returns the Unix timestamp which suffers from the Year-2038 problem on 32 bit systems it will return false on 32 bit systems when the Year-2038 problem applies (but still work on 64 bit systems).

So it is not safe to use on a 32 bit system I'd say.

Upvotes: 1

Christian Gollhardt
Christian Gollhardt

Reputation: 17024

What have you tried? What have you done, to confirm your Question?

Simple enough:

$datetime = new DateTime('5000-01-01');
var_dump($datetime->format('d.m.Y'));
var_dump($datetime->getTimestamp());

Output:

string(10) "01.01.5000"
bool(false)

So: No, you are not save in Using a TimeStamp from DateTime.

Anyway: Question might be a good reference, but can be easy be found out by testing.

The Sence about DateTime is not to get a Unix Timestamp. It is about avoiding a Unix Timestamp. See the answer from your own link from the comments: What is a Unix timestamp and why use it?

Upvotes: 1

Related Questions