1

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?

Community
  • 1
  • 1
Jack M.
  • 3,676
  • 5
  • 25
  • 36

2 Answers2

1

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?

Community
  • 1
  • 1
Christian Gollhardt
  • 16,510
  • 17
  • 74
  • 111
1

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.

Torsten Römer
  • 3,834
  • 4
  • 40
  • 53
  • 1
    With 64 bit, you will have `a bit` more time, but also this is limited. Unix Timestamps are not save! – Christian Gollhardt Jun 27 '14 at 08:51
  • I am trying to understand whether in a 32-Bit system, the getTimestamp() will return a "big" int, so I can store it as a string. – Jack M. Jun 27 '14 at 08:56
  • @Christian Gollhardt: If you don't expect your program to still be around after 15:30:08 on Sunday, 4 December 292,277,026,596 then it is perfectly safe. http://en.wikipedia.org/wiki/Year_2038_problem#Solutions – Torsten Römer Jun 27 '14 at 08:56
  • 1
    @Jack M.: According to the PHP manual, `DateTime::getTimestamp()` will return false when the Year-2038 bug applies. – Torsten Römer Jun 27 '14 at 08:59
  • 1
    @JackM. `BigInt` is something in your DataBase (You will get this as string). Php can not native handle this Datatype. `getTimestamp` returns an integer. You should realy persistent your data as `DateTime` in SQL and work on PHP Side with `DateTime` object. – Christian Gollhardt Jun 27 '14 at 09:03
  • 1
    To summarize: Better stop using Unix timestamps in PHP. Thanks! – Jack M. Jun 27 '14 at 09:06
  • 1
    @JackM. Any language. SQL, Java, C#, (even XML or JSON) – Christian Gollhardt Jun 27 '14 at 09:07