Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Question about time functions in PHP and Javascript particularly the getTime function of Javascript

I have a problem in my code, it seems like PHP and Javascript is returning different Unix Epoch time..

My code in Javascript is this:

day = 13;
month = 4;
year = 2011;
hour = 15;
minute = 10;

date = new Date(year, month - 1, day, hour, minute, 0);
dateseconds = date.getTime();

The dateseconds returns 1302678600000

But in my PHP code I use this:

$day = 13;
$month = 4;
$year = 2011;
$hour = 15;
$minute = 10;

$date = mktime($hour, $minute, 0, $month - 1, $date, $year);
$dateseconds = date('U', $date);

The dateseconds in PHP returns smaller time: 1302678600

It's similar but it seems like Javascript returns bigger time..

How to make them similar?

like image 927
Kevin Lee Avatar asked Aug 26 '26 20:08

Kevin Lee


2 Answers

Apparently Javascript returns the given time in milliseconds, while PHP gives seconds only.

Edit: so to answer your question, I think you should divide the time you get in JS by 1000, because it will always be divisible with 1000, since the smallest unit you set in your code example is in minutes.

like image 136
Imi Borbas Avatar answered Aug 28 '26 10:08

Imi Borbas


javascript returns milliseconds, and php - seconds

like image 42
CoolEsh Avatar answered Aug 28 '26 10:08

CoolEsh



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!