JavaScript-about calculating the date difference.

Source: Internet
Author: User
Tags date1 echo date
Background: I use PHP to calculate from today, postponed three months (three months is not necessarily 90 days, for example, from today's November 30, the postponement of three months is the 28th number February) date. The Java side says they're good to get this three month date.

Puzzle: Calculate the date difference between two time I know, but I don't count three months. Here's what I'm doing:

   echo  floor((strtotime('+3 month') - strtotime(date('Y-m-d')))/86400);

Solution: How to calculate the true date difference in PHP and JavaScript.

Reply content:

Background: I use PHP to calculate from today, postponed three months (three months is not necessarily 90 days, for example, from today's November 30, the postponement of three months is the 28th number February) date. The Java side says they're good to get this three month date.

Puzzle: Calculate the date difference between two time I know, but I don't count three months. Here's what I'm doing:

   echo  floor((strtotime('+3 month') - strtotime(date('Y-m-d')))/86400);

Solution: How to calculate the true date difference in PHP and JavaScript.

Moment (a JS time frame)

Document Address: http://momentjs.com/#relative-time

jsmoment("20111031", "YYYYMMDD").fromNow(); // 4 years agomoment("20120620", "YYYYMMDD").fromNow(); // 3 years agomoment().startOf('day').fromNow();        // 15 hours agomoment().endOf('day').fromNow();          // in 9 hoursmoment().startOf('hour'

Other ways to use

jsvar date1 = moment('2015-01-01')var date2 = moment('2000-01-01')date1.diff(date2)  // 473385600000date1.from(date2)  // "in 15 years"

CarbonProbably the best wheel in PHP for time processing: http://carbon.nesbot.com/docs/#api-difference

javascript// 之前项目里用到的实现方式var f = function (date, diff) {    var day;    if (typeof date == 'string') {        // yyyy-mm-dd 不是合法的格式        date = date.replace(/-/g, '/');    }    date = new Date(date);    day = date.getDate();    diff = ~~diff || 0;    date.setMonth(date.getMonth() + diff);    // 天数超出目标月的最大天数时,会顺延到下个月    // 然后设置下个月的第零天,重置到目标月的最后一天    if (date.getDate() != day) {        date.setDate(0);    }    return date;}f('2014/11/30', 3);

JS version,

var d = new Date();d.setMonth(d.getMonth() + 3);

I remember PHP has a very useful time function, you can directly add the month, PHP operation time is the simplest, ordinary add 3 month is good

SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");        Date date=new Date();        String dateString=sdf.format(date);        date.setMonth(date.getMonth()+10);        String dateString1=sdf.format(date);

====== results =====

2015-06-10  :2016-04-10

This will be compared to PHP and Java algorithm, PHP side is this understanding +n month

/** * @var $days int 总计天数 * @var $year int 当前年 * @var $month int 当前月 */$days = 0; $year = 2015; $month = 3;/** * @var $n int 几个月 */for ($i = 0; $i < $n; $i ++) {    $_days = getMonthDaysByYearAndMonth($year, $month);    $days += $_days;    $month ++;}//然后+n month就转变成 +n days了

But I do not know that Java is not such a calculation, but the logic of an analysis understand that the landlord should understand JS end how to do it.

Example date (' y-m-d ', Strtotime (' 2015-01-30 +1 month ') = = = Date (' y-m-d ', Strtotime (' 2015-01-30 +31 days ') is March 2

Is it not easy to say that your code is improved?

echo  floor((strtotime('+3 month') - time())/86400);

This is a bit of an egg, if today is February 28, Java can calculate three months later today is May 30, what about May 28? Not that language is so smart.

You want this result (based on the current date from the distance of one months 1th), PHP is also possible.

echo Date (' y-m-d ', Strtotime (' Last day of +3 month ')-(The Strtotime (' Last day of this month ')-strtotime (' Today '));

First calculate the distance from the next month to 1th, and then add 3 months at the end of the month, minus the distance.
Obviously this is possible in any language.

phpdate('Y-m-d',strtotime('-3 months'));

The question does not understand, is to "Today" postpone 3 months time?

If the delay is 3 months is the date:

phpdate('d',strtotime('-3 months'));
  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.