Whats wrong with my code

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ehpratah
    New Member
    • Mar 2012
    • 35

    #1

    Whats wrong with my code

    hi im having an error with my code can anyone help me try to fix my problem

    the error is

    Code:
    Fatal error: Call to a member function diff() on a non-object in C:\xampp\htdocs\msicfinal\samp.php on line 12
    and my code is

    Code:
    <?php
        /*These look like they are sent via form submit to this program*/
        $date1 = '2012-04-01'; // pick up date
        $date2 = '2012-05-27'; // return date
        $perday=2500;
        $permonth=52500;
        $perweek=15000;
        $datetime1 = ($date1);
        $datetime2 = ($date2);
        $interval = $datetime1->diff($datetime2); // the math for the difference
        $rest = $interval->format('%d days'); // this may contain weeks
        $week = intval($rest / 7); // weeks extracted from the above var.
        $mon = $interval->format('%m'); // The mon from formatting
        $day = intval($rest % 7); // This line MUST STAY to figure number of days
        $total = $mon * $permonth + $week * $perweek + $day * $perday;
        echo $total . '<br />';
        ?>

    the purpose of this code is to compute the rate depending on the date
  • Luuk
    Recognized Expert Top Contributor
    • Mar 2012
    • 1043

    #2
    from the manual:
    Code:
    <?php
    $date1 = '2012-04-01'; // pick up date
    $date2 = '2012-05-27'; // return date
    $datetime1 = new DateTime($date1);
    $datetime2 = new DateTime($date2);
    $interval = $datetime1->diff($datetime2);
    echo $interval->format('%R%a days');
    ?>
    note the 'new DateTime'

    Comment

    • ehpratah
      New Member
      • Mar 2012
      • 35

      #3
      hi i got it already but the problem is im getting wrong output

      here's the code

      Code:
      <?php
      
      $timezone = "Asia/Shanghai";
      if(function_exists('date_default_timezone_set')) date_default_timezone_set($timezone);
      echo date('d-m-Y H:i');
      
      /*These look like they are sent via form submit to this program*/
      $date1 = '2012-06-01'; // pick up date
      $date2 = '2012-06-01'; // return date
      
      $perday=2500;
      $permonth=52500;
      $perweek=15000;
      
      
      $datetime1 = new DateTime($date1);
      $datetime2 = new DateTime($date2);
      $interval = $datetime1->diff($datetime2); // the math for the difference
      $rest =  $interval->format('%d days');  // this may contain weeks
      $week = intval($rest / 7);              // weeks extracted from the above var.                
      $mon = $interval->format('%m');  // The mon from formatting
      $day = intval($rest % 7);      // This line MUST STAY to figure number of days
      
      echo 'week '.$week . '<br />';  // The echos are for your benifit and should be commented out or erased
      echo 'day '.$day . '<br />';
      echo ' months '.$mon .'<br />';
      
      
      $total = $mon * $permonth + $week * $perweek + $day * $perday;
      echo $total . '<br />';
      ?>

      in the date part of this code i try to put the pick up date to 2012-06-01 the same with the return date..but im having a wrong output


      it must be " 2500 "


      but instead im getting this 0 for the total

      22-06-2012 20:21
      week 0
      day 0
      months 0
      0


      and also when i change the pick up date to 2012-06-01 and the return date to 2012-06-02

      instead of getting "5000"

      im getting

      22-06-2012 20:28

      week 0
      day 2
      months 0
      2500




      sorry if im bothering you it just that i really need a hand right now..

      Comment

      • Dormilich
        Recognized Expert Expert
        • Aug 2008
        • 8694

        #4
        you may have not noted that $rest is a string, not a number and hence Math can be a little complicated.

        Comment

        • ehpratah
          New Member
          • Mar 2012
          • 35

          #5
          well i got it already..with the help of some expert..

          Comment

          • Dormilich
            Recognized Expert Expert
            • Aug 2008
            • 8694

            #6
            and what was the solution?

            Comment

            Working...