date difference calculation in PHP

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • seralasu
    New Member
    • Oct 2006
    • 34

    date difference calculation in PHP

    hi,
    I don't know how to subtract two date. Date format is (Y.m.d).
  • iam_clint
    Recognized Expert Top Contributor
    • Jul 2006
    • 1207

    #2
    [font=Arial]http://www.w3schools.com/vbscript/func_datediff.asp[/font]


    of course this is vbscript but I am pretty sure you can use the datediff() function in php aswell.

    Comment

    • seralasu
      New Member
      • Oct 2006
      • 34

      #3
      Originally posted by iam_clint
      [font=Arial]http://www.w3schools.com/vbscript/func_datediff.asp[/font]


      of course this is vbscript but I am pretty sure you can use the datediff() function in php aswell.
      Thaks,I'll try convert PHP code

      Comment

      • ronverdonk
        Recognized Expert Specialist
        • Jul 2006
        • 4259

        #4
        First convert the dots to hyphens. Then recalculate to sceonds, cal the difference in seconds and re-format to days. This is it[php]<?php
        // init values
        $datefrom = '2006.7.12';
        $dateto = '2006.7.20';
        // convert dots to hyphens
        $datefrom = str_replace('.' ,'-', $datefrom);
        $dateto = str_replace('.' ,'-', $dateto);
        // calc to seconds
        $datefrom = strtotime($date from, 0);
        $dateto = strtotime($date to, 0);
        // calc difference in seconds
        $difference = $dateto - $datefrom;
        // reformat to days
        $datediff = floor($differen ce / 86400);
        // print result
        echo "Difference is: $datediff days";
        ?>[/php]
        Ronald :cool:

        Comment

        • ctsasikumar
          New Member
          • Nov 2006
          • 17

          #5
          Use Callender function in php GregorianToJD



          $x0 = GregorianToJD($ m0,$d0,$y0);
          $x1 = GregorianToJD($ m1,$d1,$y1);

          $diff = $x1 - $x0;

          it's very simple

          Comment

          Working...