[PHP] Capacité d’agent aérien d’accueillir les voyageurs

Objectif (Version française)

Les périodes de vacances scolaires riment souvent avec fortes affluences dans les aéroports.

Une compagnie aérienne tente de répondre à ce besoin en mobilisant d’avantage d’agents d’escale prêts à accueillir et aiguiller les voyageurs. L’affluence n’étant pas constante, elle décide de mobiliser plus ou moins d’agents selon l’heure de la journée.

Pour simplifier, on considère qu’une journée est divisée en 3 parties :
– de 00h00 à 7h45
– de 7h46 à 15h45
– de 15h46 à 23h59
Une réunion a lieu chaque veille de départ pour estimer le nombre d’agents à mobiliser le lendemain sur chacune des trois périodes de la journée. Un algorithme complexe calcule une projection sur les heures d’arrivées de chaque voyageur.

En supposant qu’un agent peut répondre à 30 voyageurs sur une période donnée, vous devez estimer le minimum d’agent à mobiliser sur chaque période.

 

Format des données

Entrée
Ligne 1 : un entier N représentant le nombre de voyageurs dans une journée
Ligne 2 à N+1 : une heure H au format HH:MM correspondant à une heure d’arrivée d’un voyageur

Sortie
Trois nombres séparés par des espaces correspondant aux nombres d’agents d’escale à mobiliser sur chaque période

Objectif (Version chinoise)

航空公司要计算一天之内服务员配置问题。

一天分为三个时段,分别是:

从0点到7点45;从7点46到下午3点45;从下午3点46到晚上11点59.

每个服务员在每个时间段内最多可以接待30个旅客。

输入数据:

第一行:N为一天游客总人数

从第二行到第N行:游客出现时间点

输出数据:

计算每个时间段所需要服务员的人数。

[PHP]memory limit

 

Encountered problem :

PHP Fatal error:  Allowed memory size of 134217728 bytes exhausted (tried to allocate 32 bytes)

 

Tempory solution for debug :

Add ini_set(‘memory_limit’, ‘-1’);  in for example index.php to turn off the default memory control of PHP.

This is not the best solution, because it means that the program can use as much memory as it wants, maybe that will cause a memory overflow. The best way is to improve the program, for example pagnation to a batch etc.

[PHP] PHP code showing on the page instead of being executed

Environment :
Ubuntu Server 14.04
PHP 5.5.9

Problem :
PHP code showing on the page instead of being executed

Reason :
The reason that I met this problem is because the PHP file I used begin with (<?) instead of (<?php) which the short tag is deprecated by default.

Solution :
Find php.ini in /etc/php5/apache2, make sure the option short_open_tag=On, if not, change if to On.

Another solution more complete from stack overflow : http://stackoverflow.com/a/5121589/740546

[OpenShift] Deploy PHP/MySQL in OpenShift got 503 Service Temporarily Unavailable / 504 Gateway Time-out

Context :

The objective is to deploy the connection of MySQL with PHP in an OpenShift Server.
OpenShift is PaaS provided by Red Hat. For more info, read wikipedia OpenShift.

Problem :

The server returns :

503 Service Temporarily Unavailable

or

504 Gateway Time-out

Continue reading

[PHP] upload file and zip with password

Background (needs):
1. upload a file without checking the type (whatever the type)
2. move the file into a specific folder
3. zip the file with password

Thanks to the function move_uploaded_file, it is very simple to move file to wherever you want, of course, you must have the privilege to do that.

As it is quite simple, I put the source code directely here :
Continue reading

[PHP] PHP Fatal error can’t use method return value in write context

Context :
Development with PHP

Error message :
PHP Fatal error can’t use method return value in write context

Code generated error :

if(empty(preg_match($pattern, $subject))){
  ....
}

Error reason (from php.net) :

Note:
Prior to PHP 5.5, empty() only supports variables; anything else will result in a parse error. In other words, the following will not work: empty(trim($name)). Instead, use trim($name) == false.

Code corrected :

preg_match($pattern, $subject, $match);
if(empty( $match )){
  ....
}

[PHP] gmdate function date limit in 32 bits system

Today I got this problem when executing the gmdate in PHP.

echo gmdate('Y-m-d\T00:00:00Z', strtotime("9999-12-31T00:00:00Z"));

While normally it should return “9999-12-31T00:00:00Z”, but which made me surprised is that finally I got a “1970-01-01T00:00:00Z”.
And I tried this command in another PC, which returned me the good answer.

Where did the 1970 came from? I guessed this might be something like the start of the world or the creation of PHP or etc.

Bon, ok ! Let’s check out the difference between the version of the system and the version of PHP.

The first PC (Ubuntu)which returned me a “1970” got this :

# uname -a
Linux 2.6.32-38-generic #83-Ubuntu SMP Wed Jan 4 11:13:04 UTC 2012 i686 GNU/Linux

The second PC which returned me the “9999” got this :

#uname -a
Linux 3.2.0-29-generic #46-Ubuntu SMP Fri Jul 27 17:03:23 UTC 2012 x86_64 x86_64 x86_64 GNU/Linux

I think I got the answer, if it is a 32-bit unix-like system, the time stamp is based in seconds, and the biggest by calculating 2^32 something like that will be a number 2147483647, so nearly 68 years, and the begin year for unix time (defined by IEEE ?) is 1970, so the max is 1970 + 68 = 2038. So here if the year is bigger than 2038, there will be an integer overflow, an error of course, all… will return back to the beginning 🙂

Find more explanation in wiki :
http://en.wikipedia.org/wiki/Year_2038_problem

Find an answer in stack overflow :
http://stackoverflow.com/a/5879202

[PHP] Compare string to 0 in PHP

The question seems simple and naif but if you really do it, it’s a little annoying.

You may going to met this kind of case :

if('your_string' == 0) {
 echo "The result of 'your_string' == 0 is Equivalent\n";
} else {
 echo "The result of 'your_string' == 0 is Not Equivalent\n";
}

if('your_string' == 1) {
 echo "The result of 'your_string' == 1 is Equivalent\n";
} else {
 echo "The result of 'your_string' == 1 is Not Equivalent\n";
}

if('your_string' == '0') {
 echo "The result of 'your_string' == '0' is Equivalent\n";
} else {
 echo "The result of 'your_string' == '0' is Not Equivalent\n";
}

if('your_string' == '1') {
 echo "The result of 'your_string' == '1' is Equivalent\n";
} else {
 echo "The result of 'your_string' == '1' is Not Equivalent\n";
}

In my post, the result is :
The result of ‘your_string’ == 0 is Equivalent
The result of ‘your_string’ == 1 is Not Equivalent
The result of ‘your_string’ == ‘0’ is Not Equivalent
The result of ‘your_string’ == ‘1’ is Not Equivalent

So you see here, the first check is true, that we don’t want.
To avoid the problem, what you have to do is compare with the type, like :

if("your_string" === 0) ...

if( strval(</span>"your_string") === 0) ...

[Apache] Install configuration perl and php for apache

To install the configuration of perl and php for apache in Ubuntu, it is sufficient to execute several commands.

For perl : sudo apt-get install apache2

For php : sudo apt-get install php5 libapache2-mod-php5

After doing these commands, if we check whether the modules are avaliable, we just need a short script :

check_mod(){
    mode=$1
    mods_enabled=/etc/apache2/mods-enabled
    mods_available=/etc/apache2/mods-available

    if [ -f "$mods_enabled/$mode" ] ; then
    echo "The module $mode is enabled"
    else  
    if [ -f "$mods_avalable/$mode" ] ; then
        echo "The module $mode is available"
        sudo ln -s $mods_available/$mode $mods_enabled/$mode
    else  
        echo "[KO]The module is not installed[KO]"
    fi
    fi
}

check_mod perl.load
check_mod php5.conf
check_mod php5.load

Ref.
https://help.ubuntu.com/10.04/serverguide/php5.html
http://www.blog.highub.com/perl/install-configure-apache-localhost-perl-on-linux-ubuntu/