Difference between 2 dates using PHP

Hello

Having some problems with the calculation of the difference between 2 dates.

I noticed that about half of the results output 552 months... where is should be approximately 24 months...

I also noticed that records that display this incorrect calculation having the month following the date less than the current month, it fixed $date2 as 01/01/1970

This is the code:

<? PHP

Calculate the remaining months

$date1 = date('d/m/Y');

$date2 = $renewal_row ["enddate"];

$date2 = date (a/m/Y ", strtotime (str_replace ('. ', ' ' ', $renewal_row [' enddate ']', $renewal_row ['enddate']))");

$diff = abs (strtotime ($date2) - strtotime ($date1));

$years = floor ($diff / (365 * 60 * 60 * 24));

$months = floor (($diff-$years * 365 * 60 * 60 * 24) / (30 * 60 * 60 * 24));

$days = floor (($diff-$years * 365 * 60 * 60 * 24-$months * 30 * 60 * 60 * 24) / (60 * 60 * 24));

printf ("%d years, %d months, %d days\n", $years, $months, $days);

$months_remaining = ($years * 12) + $months;

? >

<? PHP echo "$date1 ='.strtotime ($date1).» ('. $date1.) ") date2 = '.strtotime ($date2).' ('. $date2.) ('') = Difference'. $diff;? >

The date correct difference between these dates:

$date1 = 1452038400 (06/01/2016) date2 = 1520035200 (03/03/2018) difference = 67996800

Day with incorrect difference:

$date1 = 1452038400 (06/01/2016) date2 = 1452038400 difference =(20/11/2016)

Note that if the $date2 month exceeds the month current (June) it messes up.

Any help would be appreciated!

Assuming that this ["enddate"] $renewal_row is stored in a MySQL/MariaDB DATE, DATETIME, or TIMESTAMP column.

$now = new DateTime();

$end = new DateTime ($renewal_row ['enddate']);

$time_remaining = $now-> diff ($end);

echo $time_remaining-> format (' %m month %d days ");

Tags: Dreamweaver

Similar Questions

  • Difference between 2 dates using variables in ODI

    Hello

    I have 3 variables in ODI

    Start date, end Date and duration.

    Start - alphanumeric characters - select TO_CHAR(SYSDATE,'DD-MM-YYYY HH24:MI:SS') FROM DUAL

    End Date - Alphanumeriic - select TO_CHAR(SYSDATE,'DD-MM-YYYY HH24:MI:SS') FROM DUAL

    Duration - alphanumeric characters - select to_date (# start_date, ' ' DD-MM-YYYY HH24:MI:SS)- to_date (# end_date, ' ' DD-MM-YYYY HH24:MI:SS) FROM DUAL

    It says: invalid character.

    Please let me know how can I find the difference of the start and end time date date

    Have you tried

    Select to_date ('#Start_date', ' ' DD-MM-YYYY HH24:MI:SS)-to_date ('#END_date', ' ' DD-MM-YYYY HH24:MI:SS) FROM DUAL

    See you soon

    John

  • differences between the Data Pump to back up the database and use RMAN?

    What are the differences between the Data Pump to back up the database and use RMAN? What is DISADVANTAGES and BENEFITS?

    Thank you

    Search for the backup of the database in

    http://docs.Oracle.com/CD/B28359_01/server.111/b28318/backrec.htm#i1007289

    In brief

    RMAN-> physical backup. (copies of the physical database files)

    DataPump-> logical backup. (logical data such as tables, procedures)

    Docs for RMAN-

    http://docs.Oracle.com/CD/B28359_01/backup.111/b28270/rcmcncpt.htm#

    Datapump docs

    http://docs.Oracle.com/CD/B19306_01/server.102/b14215/dp_overview.htm

    Published by: Sunny kichloo on July 5, 2012 06:55

  • How do the time difference between two dates?

    Hi all

    I use this query to get the time difference between two dates.

    Select to_timestamp ('2012-10-03 12:00 ',' YYYY-MM-DD hh)-to_timestamp ('2012-10-03 11:00 ',' YYYY-MM-DD hh) as double diff;

    but do not get the correct result.

    Thank you

    Left KEY... Left Padding of tanks.

  • How to find the difference between two dates in the presentation layer

    Hi gurus,

    Hello to everyone. Today, I came with the new requirement.


    I need to know the difference between a date and the current date in the formula column application presentation layer.by.



    Thank you and best regards,
    Prates

    Hi Navin,

    TIMESTAMPDIFF function first determines the timestamp component that corresponds to the specified interval setting. For example, SQL_TSI_DAY corresponds to the day component and SQL_TSI_MONTH corresponds to the component "month".

    If you want to display the difference between two dates in days using SQL_TSI_DAY, unlike butterflies SQL_TSI_MONTH and so on...

    hope you understand...

    Award points and to close the debate, if your question is answered.

    See you soon,.
    Aravind

  • How to calculate the difference between a Date column and the Date yesterday in the analysis (in the formula in the column)

    . For example, I need to calculate the difference between a date column, 'Table_Name '. "' Column_Name ' and (Current_Date-1). I tried different ways to do this. But nothing seems to work.

    Try the below formula.

    Replace "Time". "" Date "with your column.

    TIMESTAMPDIFF (SQL_TSI_DAY, "Time". "Date", TIMESTAMPADD (SQL_TSI_DAY-1, CURRENT_DATE))

    Thank you!

  • difference between two dates in the year/month/day in Obiee

    Hi gurus,

    I know this question have been asked and answered several times but I have a requirement that is a little different, then the previous ones.

    I want to calculate the difference between two dates in OBIEE10g in year/month/day format similar to the below SQL output

    SQL > select end_date, start_date,

    trunc (months_between (end_date, start_date) / 12) years.

    months of mod (trunc (months_between (end_date, start_date)), 12).

    End_date - add_months (start_date, trunc (months_between (end_date, start_date))) days

    t

    Thanks in advance

    SK

    Search for this

    "TIMESTAMPDIFF IN THE FORM OF MTHS # YEARS."

    or else

    To get the current mandate of employees since the date of hiring in the form of # years # mths or # year (s) # month (s)

    Concat (concat (cast (TIMESTAMPDIFF (SQL_TSI_Month, "Employee attributes". (((' ' Hiring last Date employee ", CURRENT_DATE) / 12 as char),"Year (s)"), concat (cast (MOD (TIMESTAMPDIFF (SQL_TSI_Month,"Employee attributes". (((((' ' Hiring last Date employee ", CURRENT_DATE), 12) as char), 'Month (s)'))

    Concat (concat (CAST (TIMESTAMPDIFF (SQL_TSI_YEAR, "Employee attributes". (((' ' Hiring last Date employee ", CURRENT_DATE) as CHAR),"Year (s)"), concat (cast (MOD (TIMESTAMPDIFF (SQL_TSI_Month,"Employee attributes". (((((' ' Hiring last Date employee ", CURRENT_DATE), 12) as char), 'Mth (s)'))

  • difference between 2 dates

    Hello experts,

    is it possible to get 'normal' date format then tell you the difference between two dates?

    For example, I first_unit_start_date (2012.02.17 13:57) and a last_unit_completion_date (2012.02.20 07:23). is it possible to get the calculation answer 17:26?

    Now, I found the only way to get minutes with calculation ROUND (1440 * (last date start date and end-first unit), 2)

    Thanx

    Hello

    You may be able to do by creating a date where the time part holds the difference. Something like:

    To_char (trunc (sysdate) + (last_unit_start_date - first_unit_start_date), "HH24")

    Rod West

  • How to know the difference between the Dates in Oracle.

    Hello

    Suppose we have two Dates say November 5, 2005 and October 12, 2007.

    Is it possible to know the total difference between these Dates in Oracle.

    Thank you

    You can subtract date-objects. The result is the difference in days.

    select  to_date('12-10-2007','DD-MM-YYYY') -  to_date('05-11-2005','DD-MM-YYYY')   from dual;
    

    In your example: 706 days

    Published by: hm on 25.10.2010 12:33

    Please look also for "Oracle Date arithmetic" in the Oracle Documentation or Google.

  • Find the difference between two date and time

    Hi friends,

    I wanted to find the difference between two date and time, but my qury is slightest error "invalid number."

    select sql_step_num,proc_name,run_seqno,start_date,end_date,(to_char(start_date,'HH24-MI-SS') - to_char(end_date,'HH24-MI-SS') ) as ed  
    from eval.EVAL_RUNTIME_DETAILS
    where trunc(start_date) = trunc(sysdate) 
    order by sql_step_num;

    You try to get the feel between two char strings.
    And more difference between two dates gives a NUMBER of days.
    Try this:

    select sql_step_num,proc_name,run_seqno,start_date,end_date,numtodsinterval(end_date-start_date,'DAY') as ed
    from eval.EVAL_RUNTIME_DETAILS
    where trunc(start_date) = trunc(sysdate)
    order by sql_step_num;
    
  • Calculation of the difference between the dates.

    All,
    I have a requirement where I need to calculate the difference between the dates. If the given date is less than the current date, it should return '0' (zero) other wise should return 1. How can I do this?

    Kind regards
    Vinciane

    and with a CASE statement...

    SQL> ed
    Wrote file afiedt.buf
    
      1  select case when to_date('01/11/2008','DD/MM/YYYY') /
    
      DATECOMP
    ----------
             0
    
    SQL>
    
  • Difference between two dates in the server behaviors

    Hello all,.

    I am trying to create a Recordset by using server behaviors and that I'm not an expert in php, I'm completely lost...

    The problem is this: I want to find in a table of all the records where the difference between today's date and date of creation (stored in this table, although course) is superior to, say 30 days. How can I specify the variable field?

    Thank you very much in advance for your help!

    You must use a sql like query below:

    SELECT * FROM table WHERE creationDate< date_sub(now(),="" interval="" 30="">

    'table' is the name of the table that you want to select the information of and 'creationDate' being the column 'date' in the database

  • Difference between available and used space

    Dear team,

    can someone please explain to me the difference between the provision and the disk space used in simple words.

    concerning

    Mr. VMware

    The summary above capacity data store is interpreted as follows: -.

    The capacity is the total size.

    Put into service space is the total size allocated to virtual machines.

    Free space is the space not exhausted by VMs again.

    Therefore, with a provisioning, you can provision the amount of space to virtual machines based on the free space at the time there. A virtual machine can be allocated to 100 GB but only use 50 GB. The unused 50 GB remaining is counted as part of the free space. This can be seen also by clicking on the tab "Virtual Machines" of the data store to see the "put into service space' and 'space used '.

    I hope this helps.

  • Difference between two dates

    Hello

    I have two columns both of them date data types.

    I would like to know the difference between these two dates.

    SELECTION INTERVAL ' + 2342 23:23:23 ' DAY (4) TO THE SECOND - INTERVAL ' + 102 13:13:13 ' DAY (4) SECOND FROM DUAL;

    But, I get this:

    '+ 2240 10:10:10.000000'

    I'd like to see the result in a year-month and jour-deuxieme groups. I can't do anything with + 2240 days...

    Thanks in advance!

    Hello Adme12

    You can use it.

    SET LINESIZE 110 PAGESIZE 20

    COLUMN today FORMAT A20

    COLUMN TOMORROW FORMAT A20

    COLUMN YEAR_MONTHS FORMAT A15

    COLUMN DAY_SEC FORMAT A30

    SET ECHO ON

    SELECT TO_CHAR (today, ' JJ.) MR. HH24:MI:SS' YYYY) LIKE TODAY

    , TO_CHAR (tomorrow, ' JJ.) MR. HH24:MI:SS' YYYY) TOMORROW

    NUMTOYMINTERVAL (MONTHS_BETWEEN (TOMORROW, TODAY), 'MONTHS') AS YEAR_MONTHS

    , NUMTODSINTERVAL (TOMORROW - ADD_MONTHS (TODAY, TRUNC (MONTHS_BETWEEN (TOMORROW, TODAY))), 'DAY') AS DAY_SEC

    Of

    (SELECT SYSDATE + INTERVAL ' + 102 13:13:13 ' DAY (4) ON THE SECOND THAT TODAY ' HUI)

    , SYSDATE + INTERVAL ' + 2342 23:23:23 ' DAY (4) ON THE SECOND TOMORROW

    OF THE DOUBLE

    )

    ;

    TODAY TOMORROW YEAR_MONTHS DAY_SEC

    -------------------- -------------------- --------------- ------------------------------

    23.04.2014 04:49 10.06.2020 14:59:10 + 000000006-02 + 000000018 10:10:10.000000000

    Of course, it is not so good that you have so many 0, but you can convert the result to a char (TO_CHAR) and you can enter.

    I hope this helps you!

    Kind regards

    David

  • How to find the difference between two dates in time except Sunday

    Hi all

    I have a table, as shown below.
    SQL> select * from test;
    
    TR_ID                                              CREATE_TIME                                                                       CODE
    -------------------------------------------------- --------------------------------------------------------------------------- ----------
    S12341                                             05-JUN-12 12.20.52.403000 AM                                                      1003
    S12342                                             11-JUN-12 11.15.33.182000 AM                                                      1003
    S12342                                             07-JUN-12 12.00.36.573000 PM                                                      1002
    S12343                                             20-JUN-12 12.34.37.102000 AM                                                      1003
    S12343                                             15-JUN-12 11.34.27.141000 PM                                                      1002
    S12344                                             01-JUL-12 10.01.06.657000 PM                                                      1002
    S12344                                             06-JUL-12 12.01.04.188000 AM                                                      1003
    S12341                                             31-MAY-12 11.20.38.529000 PM                                                      1002
    I would like to know the difference between same tr_ids create_time, which should give out in hours except Sunday.

    For example:

    TR_ID: S12344
    1002_Create_time: July 1, 12 PM 10.01.06.657000 (i.e. Sunday)
    1003_Create_time: 12.01.04.188000 AM 6 July 12

    1002 create time is 22:00 Sunday.

    If the query must exclude only the hours of Sunday which is 10 p.m. to Monday 00 h which is 2 Hrs.

    I tried the sub query after doing a search on this forum but I am not getting the desired output.
    SELECT count(*) FROM (SELECT ROWNUM RNUM,tr_id,create_time CT_1002 FROM test c WHERE c.tr_id='S12344' and 
    ROWNUM <= (select (cast(a.create_time as date)-cast((select create_time from test b where a.tr_id=b.tr_id and code=1002) as date)) 
    from test a where a.code=1003 and a.tr_id=c.tr_id) + 1) d 
    WHERE to_char(cast((select create_time from test e where e.tr_id=d.tr_id and code=1002) as date) + RNUM - 1, 'DY') NOT IN('SUN');
    Need help to get the desired o/p

    Hello

    If I unederstand the problem correctly, that's what you want:

    WITH       got_extrema     AS
    (
         SELECT       tr_id
         ,       CAST (MIN (create_time) AS DATE)     AS start_date
         ,       CAST (MAX (create_time) AS DATE)     AS end_date
         FROM       test
         GROUP BY  tr_id
    )
    SELECT       tr_id
    ,       start_date, end_date          -- If wanted
    ,       24 * ( ( ( TRUNC (end_date,   'IW')          -- Count -1 day for every full week
                        - TRUNC (start_date, 'IW')
                 )
               / -7
                  )
                + LEAST ( end_date               -- If end_date is a Sunday
                            , TRUNC (end_date, 'IW') + 6     -- consider it 00:00:00 on Sunday
                     )
                - CASE
                          WHEN  start_date >= TRUNC (start_date, 'IW') + 6     -- If start_date is a Sunday
                   THEN  TRUNC (start_date, 'IW') + 6               -- consider it 00:00:00 Sunday
                   ELSE  start_date
                      END
                )     AS total_hours
    FROM      got_extrema
    ;
    

    I guess that you don't need to worry about fractions of a second. As you did in the code you have posted, I am to convert the TIMESTAMP to date values, because of DATE arithmetic and functions are so much better than what is available for timestamps.

    Basically, it's just to find the number of days between start_date and end_date and multiplying by 24, with these twists:
    (a) 1 day is deducted for each week between start_date and end_date
    (b) if End_date is a Sunday, none of the end_date himself hours are counted
    (c) If start_date is a Sunday, then all the start_date himself hours are counted. Why these hours should be counted? Because 1 day is already being deducted for the week which includes start_date, which contains only this Sunday.

    TRUNC (dt, 'IW') is the beginning of the ISO week containing dt; in other words, 00:00:00 the or before the dt last Monday. This is not the NLS parameters.

    Of course, I can't test without some sample data and the exact results you want from these data. You may need a little something more If start_date and end_date are both on the same Sunday.
    Whenever you have a problem, please post a small example of data (CREATE TABLE and only relevant columns, INSERT statements) of all of the tables involved.
    Also post the results you want from this data, as well as an explanation of how you get these results from these data, with specific examples.
    Always tell what version of Oracle you are using.
    See the FAQ forum {message identifier: = 9360002}

Maybe you are looking for