Search code examples
phpmamplamp

Syntax difference between MAMP and Ubuntu Server


I have the following function to account for Sundays in business day calculations. It works on my test server which is MAMP running on a Mac. When I move the code to an Ubuntu server running LAMP, it stops working (manifested as the page not loading anything).

I can't work out why, and have tried all different combinations of single quotes and double quotes.

The function is:-

// function to account for Sundays, and public holidays. Add holiday dates in $holidayDays variable
function get_next_business_date($from, $days) {
$workingDays = [1, 2, 3, 4, 5, 6]; # date format = N (1 = Monday, ...)
$holidayDays = ["*-12-25","*-12-26", "*-01-01", "2014-12-24"]; # variable and fixed holidays

$from = new DateTime($from);
while ($days) {
    $from->modify("+1 day");
    if (!in_array($from->format('N'), $workingDays)) continue;
    if (in_array($from->format('Y-m-d'), $holidayDays)) continue;
    if (in_array($from->format('*-m-d'), $holidayDays)) continue;
    $days--;
}
return $from->format("Y-m-d"); #  or just return DateTime object
}

$today = date("Y-m-d", strtotime("today"));
$tomorrow = get_next_business_date("today", 1);
$twodays = get_next_business_date("today", 2);
$yesterday  =   date("Y-m-d", strtotime("yesterday"));

I'm lost as to why this could be.


Solution

  • As per the responses in the various comments, this was a server version issue. The version of PHP on my test box was more recent than the version on the live.

    If it's of any use, I am using MAMP on a Mac Mini as my test environment which had 5.5.3, and LAMP on an UBUNTU 12.04 server box which had 5.3. I didn't want to upgrade the Ubuntu version, just the PHP version and did so as per this http://phpave.com/upgrade-php-5-3-php-5-5-ubuntu-12-04-lts/

    Things to be aware of (if you don't know) are that the default directory for your php code will change from /var/www to /var/www/html. You can either move your PHP to the /var/www/html folder or change the default folder as per the link above. I suspect that changing the default may have repercussions to future versions as they will be expecting code to reside in /var/www/html.

    There will also be some permissions issues for that folder that you may wish to be aware of if you are copying into and creating sub folders.

    Hope this is of use to someone.