简体   繁体   中英

Difference in minutes between two hours

The goal

Get the minutes' difference between two hours, like: 12:20:00 and 12:15:00 . The result is: 5 .

The problem

I have a string in PHP that "houses" some time, like the example above. And with JavaScript, I need to enable a button when the minutes' difference is greater than 3. In other words, this is what I have in PHP: 10:00:00 and this is what the Date() object brings to me in JavaScript: Thu Oct 10 2013 11:29:49 GMT-0300 (Hora oficial do Brasil) .

I need to get the 11:29:49 and compare with 10:00:00 to get the minutes' difference. If this difference is greater than 3, then do something .

Code spotlight

This is what I'm trying, but doesn't work , of course. It's impossible to calculate the difference because the variable's type are different.

<script>
var date = new Date();
    nowInHours = date.getHours() + ":" 
                 + date.getMinutes() + ":" 
                 + date.getSeconds(),
    fantasyHours = <?php $application->time(); ?>;

console.log(fantasyHours - nowInHours);
</script>

Can someone give me an idea?

First of all, manage your time like a boss. Times in programming language are calculated as the number of second or millisecond since a setted date (what we call a Unix timestamp - 1 January 1970 00:00:00 UTC ).

As you'll understand, having a date in this standard format is the best as you simply have two integer you can compare.

In JavaScript and PHP, these date format are the default and very easy to use.

JS: new Date().getTime()
PHP: time() and the date constructor (But here I can't help much, the business logic of your application is only known by you)

So, convert your time from PHP in a convenient standard format (the UTC format). Then use it to share dates between languages and compare them together.

Convert both dates to miliseconds, and compare those. If they are within 18000, they they are within 3 minutes. Date.getTime() in js will do this for you.

You can attempt to convert your PHP date to a Javascript date before working on it with the help of some Regex or String.split function.

See Javascript Date

Can you not use JodaTime, convert both you dates to LocalDateTime then compare? This appears to work:

public static void main(String[] args) {

    LocalDateTime date1 = new LocalDateTime(2013,10,10,12,15,0);
    LocalDateTime date2 = new LocalDateTime(2013,10,10,12,20,0);

    assertEquals( 5, Minutes.minutesBetween(date1, date2).getMinutes() );
}

var fantasyHours = new Date(<?php echo $application->time(); ?> * 1000)

That should give you the time from PHP to JavaScript. You can now check both date objects to see the diffrence.

The *1000 is needed because the PHP unix time is in seconds and new Date() needs miliseconds.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM