Syntax difference between MAMP and Ubuntu Server - php

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.

you may have version changes between the machines.
on ubuntu, you can check the file /var/log/apache2/error.log
tail -f /var/log/apache2/error.log
to see what error was thrown.
To check for the current PHP version on your machine, you could use
php -v
from the command line (requires php5-cli module installed)
another way is to put a PHP file with:
<?php
phpinfo();
?>
in your web dir, and open it with your browser.

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.

Related

PHP ngettext() error on Macbook M1 - 13.1 - MAMP Server

I have an fatal error that breaks my website and output
500 Internal Server Error
I did debug the issue and found out that the former dev used ngettext to get the singular or plural of text.
$day = 2;
printf(ngettext("%d day", "%d days", $day, $day);
// Expect output: 2 days
I have check all the log, no log in PHP, but there is one in Apache log everytime I try to run ngettext() somewhere in my source.
The process has forked and you cannot use this CoreFoundation functionality safely. You MUST exec().
Break on __THE_PROCESS_HAS_FORKED_AND_YOU_CANNOT_USE_THIS_COREFOUNDATION_FUNCTIONALITY___YOU_MUST_EXEC__() to debug.
I did:
Check the gettext module enabled.
I run the code on PHP CLI:
$ php -r 'printf(ngettext("%d day", "%d days", 2, 2);'
// Output: 2 days
Deploy the source code into hosting server, it ran well.
Deploy the source code into another Macbook with Intel chip, v12. Ran well.
What am I missing? Please advise me. TIA

Get Apache installation path via PHP in Windows environment

How do I use PHP 8.0 to determine the Apache installation path in a Windows environment?
No: I know where it is manually, I need to do this programmatically.
There was nothing in $_SERVER and I had to resort to digging through phpinfo(). The following allows you to reuse phpinfo() as $pinfo if you need to find other bits of information. Additionally PHP does not support the JavaScript index style (e.g. explode()[index]) on I think 7.2 and older so anyone stuck on those older versions may have to re-code the two explode in to separate lines.
//If you need additional items you can refer ONCE to $pinfo:
ob_start();
phpinfo();
$pinfo = ob_get_contents();
ob_end_clean();
if (PHP_OS == 'WINNT')
{
//explode()[index] will not work on PHP ~7.2 and older
echo explode('<', explode('>Server Root </td><td class="v">', $pinfo, 2)[1], 2)[0];
}

Could unlink(__FILE__) delete multiple files and folders?

There is a line of code in a plugin I've constructed that contains these lines:
if (time() > 1459382400) {
unlink(__DIR__ . '/specific-subfolder-name/specific-file.class.php');
unlink(__FILE__);
}
Is there any way either one of these commands could delete multiple files and folders?
If so, how would it happen?
Potentially relevant details:
1. I do not know the OS or web server (Windows, Linux, Mac, Apache, IIS, ???)
2. That timestamp is March 31, 2016 (this question is posted / the code ran March 25, 2016)
3. This code is not in any loop, recursion, or other mechanism that would have caused it to run multiple times.
4. This code lives in /wp-plugins/plugin-name/plugin-file.php. Files in root, as well as /wp-content/* and /wp-admin/* allegedly got vaped.

Correctly setting date.timezone on Windows 7 for PHP 5.4.35 binary only install

I am writing unit tests for a series of PHP classes in NetBeans (testing with PHPUnit).
These classes rely on a nusoap script that I don't want to mock.
When I run tests that rely on nusoap as a dependency, I get the seemingly common "It is not safe to rely on the system's timezone" error for this method in the nusoap script:
function getmicrotime() {
if (function_exists('gettimeofday')) {
$tod = gettimeofday();
$sec = $tod['sec'];
$usec = $tod['usec'];
} else {
$sec = time();
$usec = 0;
}
return strftime('%Y-%m-%d %H:%M:%S', $sec) . '.' . sprintf('%06d', $usec);
}
So, I have gone into my php.ini file and added:
[Date]
; Defines the default timezone used by the date functions ;
; http://php.net/date.timezone
date.timezone = "America/New_York"
I am only using PHP binaries. There is no web instance so there is only 1 ini file to modify, as far as I know.
When I rerun these tests I get errors from PHPUnit right away and no tests run. If I remove the edit to the ini file my tests run normally. "Normally" is defined as the tests return a failure with the same timezone error
Many of the other answers around the web suggest editing the ini file in two places, one for the web and one for the CLI. However, since I have only 1 ini file, I am not sure what else I need to modify in order to get the time zone correctly recognized.
How, on a Windows 7 machine with PHP 5.4.35 binaries installed (no IIS at all), would I make this work? I've also tried adding date_default_timezone_set( 'America/New_York' ); to the nusoap script, but this seemingly had no effect.
Thanks.
This was a red herring, and a tough one to find too.
I went back to basics and dug around in my PHP.INI file and discovered I was using the test/prod version, so I decided to try the development version.
Running the tests in both NetBeans and the CLI, I was now able to see the "real" root cause, which was related to the class I was testing. There's just no information at all displayed to the user unless configured in PHP.INI.

PHP command line error : Timezone database is corrupt

My date.php is -
<?php
echo date('Y');
When I execute php -f date.php on my staging machine, I get error -
PHP Fatal error: date(): Timezone database is corrupt - this should *never* happen!
in /home/staging/test/date.php on line 2
But when I execute the same on my local / dev machine it works. Although on both staging and local machines, permissions on /etc/localtime and /usr/share/zoneinfo/ are the same.
But output of file /etc/localtime vary on both machines.
Local (php5.3.5) :
/etc/localtime: timezone data, version 2, 4 gmt time flags, 4 std time flags, no leap seconds, 4 transition times, 4 abbreviation chars
Staging (php5.3.10) :
/etc/localtime: timezone data, version 2, 1 gmt time flag, 1 std time flag, no leap seconds, no transition times, 1 abbreviation char
While trying to find what the issue is I found this link from SO. I am confused over the accepted answer. My apache user is not executing the script.
What seems to be the problem? How do I resolve this issue?
For various reasons, PHP ships with its own timezone database - it is possible to use the system TZDB by configuring the software differently at compile time.
The PHP timezonedb is implemented as C code (lots of defines) - hence you need to recompile PHP to get it working.
If your PHP interpreter is not explicitly configured to use the OS timezoneDB, then you really need to investigate why the executable is corrupt.
Please check permission of your timezone files.It might have changed when you have install some rpms.
change the permission to 655
Also check following files:
/usr
/lib
/share
/etc
My problem was that I was running php-fpm in chroot mode. I changed it to run without a chroot, and then the error went away.

Categories