0

In JavaScript I am using (new Date("1985-05-01")).getTime(); to get the timestamp. The return value is 483753600000.

In PHP I would like to return the year of that timestamp.

date('Y', 483753600000); returns 17299 but 1985 is expected.

What am I doing wrong?

Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
Reza Saadati
  • 1,224
  • 13
  • 27

1 Answers1

3

JavaScript's getTime() will return timestamp in miliseconds. PHP's date() function needs timestamp in seconds. So just divide it by 1000:

date('Y', 483753600000 / 1000);
cagri
  • 807
  • 8
  • 17
  • or take off the 0s o.O – treyBake Sep 18 '19 at 13:35
  • @treyBake so.. divide it by / 1000? If that is a variable you'd need to convert it to a string and then cut last 3 characters. I don't see how is that any better than dividing – cagri Sep 18 '19 at 13:37
  • if it's a variable - current OP code != variable, so it's just an extra function call on something that doesn't look necessary – treyBake Sep 18 '19 at 13:41
  • Well, if "current op code is not a variable" is your logic, then on his code there's this part "1985-05-01", he can just get the first part. why bother? – cagri Sep 18 '19 at 13:45
  • my point exactly^^ – treyBake Sep 18 '19 at 13:45
  • there's a reason he's doing it with two different languages. – cagri Sep 18 '19 at 13:46
  • Or, they're using JS as an example - they have a semi-ambigious sentence about using that timestamp, but it OP code example doesn't really convey usage of that timestamp – treyBake Sep 18 '19 at 13:46
  • do you use javascript when looking at the date? – cagri Sep 18 '19 at 13:47
  • Depends, if client side, sure, if server-side then strictly php – treyBake Sep 18 '19 at 13:47
  • and how do you do that? do you divide by 1000, or just manually delete zeroes from the end? – cagri Sep 18 '19 at 13:48
  • I make sure the logic is handled in relevant place so processing is done with as little logic possible. It should already be in the correct format for PHP to just process to begin with – treyBake Sep 18 '19 at 13:49