Connecting Tech Pros Worldwide Forums | Help | Site Map

Why do I get an parse error in the first line? Please help. Thanks in advance.

Newbie
 
Join Date: Oct 2009
Location: Benoni, South Africa
Posts: 3
#1: Oct 10 '09
Expand|Select|Wrap|Line Numbers
  1. <?php
  2.  
  3. int DMYToExcelSerialDate(int nDay, int nMonth, int nYear)
  4. {
  5.     // Excel/Lotus 123 have a bug with 29-02-1900. 1900 is not a
  6.     // leap year, but Excel/Lotus 123 think it is...
  7.     if (nDay == 29 && nMonth == 02 && nYear==1900)
  8.         return 60;
  9.  
  10.     // DMY to Modified Julian calculatie with an extra substraction of 2415019.
  11.     long nSerialDate =
  12.             int(( 1461 * ( nYear + 4800 + int(( nMonth - 14 ) / 12) ) ) / 4) +
  13.             int(( 367 * ( nMonth - 2 - 12 * ( ( nMonth - 14 ) / 12 ) ) ) / 12) -
  14.             int(( 3 * ( int(( nYear + 4900 + int(( nMonth - 14 ) / 12) ) / 100) ) ) / 4) +
  15.             nDay - 2415019 - 32075;
  16.  
  17.     if (nSerialDate < 60)
  18.     {
  19.         // Because of the 29-02-1900 bug, any serial date
  20.         // under 60 is one off... Compensate.
  21.         nSerialDate--;
  22.     }
  23.  
  24.     return (int)nSerialDate;
  25. }
  26.  
  27.  
  28. print (nSerialDate);
  29.  
  30. ?>

Markus's Avatar
Moderator
 
Join Date: Jun 2007
Location: York, England, with wolves.
Posts: 4,947
#2: Oct 11 '09

re: Why do I get an parse error in the first line? Please help. Thanks in advance.


Because that is not PHP code. PHP is not a strongly type language, ergo variables and functions do not need (cannot have) their types declared.

Consider the following:

Expand|Select|Wrap|Line Numbers
  1. // valid way of declaring a function - notice the 'function' keyword.
  2. function my_function($param1, $param2) {}
  3. // invalid way of declaring a function - lacks the 'function' keyword and also specifies return type of function
  4. int my_function($param1, $param2) {}
  5. // also invalid, same as above, but also specifies type of parameters (please see next example also)
  6. int my_function(int $param1, int $param2) {}
  7. // you can 'type-hint' certain values for your parameters, such as 'array'
  8. // and classes
  9. function my_function(array $param1, my_class $param2) {}
  10.  
  11. // correct way to define a variable
  12. $my_variable = 1;
  13. // incorrect
  14. int $my_variable = 1;
  15.  
Hope this helps,
Mark.

P.S. Welcome to the forum!
P.P.S Please use code tags when posting code to the forum. That can be done as [code] your code here [/code].
Newbie
 
Join Date: Oct 2009
Location: Benoni, South Africa
Posts: 3
#3: Oct 11 '09

re: Why do I get an parse error in the first line? Please help. Thanks in advance.


Thanks for the reply Mark,
I simply just wanted to convert a Date YYYYMMDD to an Excel serial date, using .php code. I'm not having much success as I am new to .php.
Markus's Avatar
Moderator
 
Join Date: Jun 2007
Location: York, England, with wolves.
Posts: 4,947
#4: Oct 11 '09

re: Why do I get an parse error in the first line? Please help. Thanks in advance.


Quote:

Originally Posted by silvergost View Post

Thanks for the reply Mark,
I simply just wanted to convert a Date YYYYMMDD to an Excel serial date, using .php code. I'm not having much success as I am new to .php.

Read some tutorials, and you'll be able to do it yourself based on the above algorithm.
Newbie
 
Join Date: Oct 2009
Location: Benoni, South Africa
Posts: 3
#5: Oct 11 '09

re: Why do I get an parse error in the first line? Please help. Thanks in advance.


Thanks for the confidence in me. At least I can gather from your reply that what I whant to do is possible.
Markus's Avatar
Moderator
 
Join Date: Jun 2007
Location: York, England, with wolves.
Posts: 4,947
#6: Oct 11 '09

re: Why do I get an parse error in the first line? Please help. Thanks in advance.


Quote:

Originally Posted by silvergost View Post

Thanks for the confidence in me. At least I can gather from your reply that what I whant to do is possible.

Sure it is - all you're really doing is some math, which PHP handles just like any other language. I would give you a head start, but I have to run out.

Mark.
Reply