Keyne Viana
Keyne Viana

Reputation: 6202

Replicate Java's getTime() in PHP

I have the following code in java which returns 549255600000:

Date date = new Date(87, 4, 29);
long micro = date.getTime();
System.out.println(micro);

The following in PHP returns another value 546663600000:

var_dump(strtotime('1987-04-29') * 1000)

I need a php function that returns the same of the Java code. Why it's different?

The above in Java prints the same of PHP:

  // date variable is the same
  SimpleDateFormat formatter = new SimpleDateFormat("yyyy/MM/dd");
  utilDate = formatter.parse(date);
  System.out.println("utilDate: " + utilDate.getTime());

Maybe the error is in the first java snippet.

Upvotes: 3

Views: 243

Answers (1)

Keyne Viana
Keyne Viana

Reputation: 6202

As stated by @ndsmyter the problem here is that the first java snippet should be 3 in the month parameter because the month is 0 to 11. So it will output 546663600000 as in the PHP snippet.

Date date = new Date(87, 3, 29);
long micro = date.getTime();
System.out.println(micro);

Upvotes: 1

Related Questions