473,395 Members | 2,689 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

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

Expand|Select|Wrap|Line Numbers
  1. 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

Expand|Select|Wrap|Line Numbers
  1. <?php
  2.     /*These look like they are sent via form submit to this program*/
  3.     $date1 = '2012-04-01'; // pick up date
  4.     $date2 = '2012-05-27'; // return date
  5.     $perday=2500;
  6.     $permonth=52500;
  7.     $perweek=15000;
  8.     $datetime1 = ($date1);
  9.     $datetime2 = ($date2);
  10.     $interval = $datetime1->diff($datetime2); // the math for the difference
  11.     $rest = $interval->format('%d days'); // this may contain weeks
  12.     $week = intval($rest / 7); // weeks extracted from the above var.
  13.     $mon = $interval->format('%m'); // The mon from formatting
  14.     $day = intval($rest % 7); // This line MUST STAY to figure number of days
  15.     $total = $mon * $permonth + $week * $perweek + $day * $perday;
  16.     echo $total . '<br />';
  17.     ?> 

the purpose of this code is to compute the rate depending on the date
Jun 22 '12 #1
5 2327
Luuk
1,047 Expert 1GB
from the manual:
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. $date1 = '2012-04-01'; // pick up date
  3. $date2 = '2012-05-27'; // return date
  4. $datetime1 = new DateTime($date1);
  5. $datetime2 = new DateTime($date2);
  6. $interval = $datetime1->diff($datetime2);
  7. echo $interval->format('%R%a days');
  8. ?>
  9.  
note the 'new DateTime'
Jun 22 '12 #2
hi i got it already but the problem is im getting wrong output

here's the code

Expand|Select|Wrap|Line Numbers
  1. <?php
  2.  
  3. $timezone = "Asia/Shanghai";
  4. if(function_exists('date_default_timezone_set')) date_default_timezone_set($timezone);
  5. echo date('d-m-Y H:i');
  6.  
  7. /*These look like they are sent via form submit to this program*/
  8. $date1 = '2012-06-01'; // pick up date
  9. $date2 = '2012-06-01'; // return date
  10.  
  11. $perday=2500;
  12. $permonth=52500;
  13. $perweek=15000;
  14.  
  15.  
  16. $datetime1 = new DateTime($date1);
  17. $datetime2 = new DateTime($date2);
  18. $interval = $datetime1->diff($datetime2); // the math for the difference
  19. $rest =  $interval->format('%d days');  // this may contain weeks
  20. $week = intval($rest / 7);              // weeks extracted from the above var.                
  21. $mon = $interval->format('%m');  // The mon from formatting
  22. $day = intval($rest % 7);      // This line MUST STAY to figure number of days
  23.  
  24. echo 'week '.$week . '<br />';  // The echos are for your benifit and should be commented out or erased
  25. echo 'day '.$day . '<br />';
  26. echo ' months '.$mon .'<br />';
  27.  
  28.  
  29. $total = $mon * $permonth + $week * $perweek + $day * $perday;
  30. echo $total . '<br />';
  31. ?> 

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..
Jun 22 '12 #3
Dormilich
8,658 Expert Mod 8TB
you may have not noted that $rest is a string, not a number and hence Math can be a little complicated.
Jun 23 '12 #4
well i got it already..with the help of some expert..
Jun 25 '12 #5
Dormilich
8,658 Expert Mod 8TB
and what was the solution?
Jun 25 '12 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Chris Geerdink | last post by:
combo with PHP. what is wrong with the Javascript? else { include("mysql.php"); $query1 = mysql_query("INSERT INTO gbook (naam, email, text) VALUES ('".$_POST."', '".$_POST."',...
4
by: asdf | last post by:
Hello! Can someone tell me whats wrong with this piece of code: Option Compare Database Option Explicit Sub retrieve() Dim rst As ADODB.Recordset Dim i As Integer
5
by: Alexandre Martins | last post by:
Provider=Microsoft.Jet.OLEDB.4.0;UserId=Admin;Password=teste;Data Source=C:\Inetpub\wwwroot\inktoner\dados\db_inktoner.mdb;Persist Security Info=True I can't connect in my database ! whats...
1
by: aa | last post by:
When I am reading from local disk (d:), everithing is OK, but then I am reading from map disk I am geting the this error. Whats wrong. Thanks Server Error in '/Extra' Application....
3
by: mahsa | last post by:
Hi do you know whats wrong with this code? <asp:HyperLink id="HLink_Help" runat="server" NavigateUrl='<%# "javascript:window.open('comments.aspx?id=1,width=500,height=600, scrollBars=yes');"...
4
by: blah | last post by:
Hello everyone, Ive been trying to get my application to "click" on a button in another application using SendMessage, Ive gotten this far but Im not sure whats wrong with this code, here is the...
1
by: '~=_Slawek_=~' | last post by:
$DOW = (jddayofweek(unixtojd(mktime(1, 1, 1, $month, $day, $year)))+6)%7; $DOW= (jddayofweek(juliantojd($month, $day, $year))+6)%7; The results are supposed to be the same, but they are not....
7
by: Mike Barnard | last post by:
It's a simple test... VERY SIMPLE. But... In an external stlyesheet some attributes don't show. With the same styles cut and pasted to the test internally it works as expected. Anyone tell...
5
by: islayer | last post by:
can someone tell me what is wrong with the bold code? i am just learning perl. the program should create a perl file with a random name (5 letters, followed by a number), but the name is always just...
5
by: hiqu | last post by:
This issue is driving me nuts and not able to figure out whats wrong. I've this code in my firefox extension. Firefox always hangs and reports the script is busy. if I introduce a break...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.