PHPExcel for loop timeout - php

I have this code where I want to set from 3 to 333 to have a certain row height at every 30 interval, meaning at row 3, 33, 63, 93 up till 333:
for ($i=3; $i<340; $i+30){
$objPHPExcel->getActiveSheet()->getRowDimension($i)->setRowHeight(57);
}
But when I click on controller actionGenerate(), it doesn't generate and timeouts me after 30 seconds. Can I know why isn't it generating?
If I were to use the manual way, as in doing line by line, PLUS having so many different kinds of settings, I'd be exhausted.

PHP limits the execution time of any script. The default time is 30 seconds.
Your script probably takes longer than 30 seconds to execute.
Set your set_time_limit to 0.
This will set it to unlimited.
like this:
set_time_limit(0);
for ($i=3; $i<340; $i+=30){ // <--- here was your problem
$objPHPExcel->getActiveSheet()->getRowDimension($i)->setRowHeight(57);
}
And try again.
Info about set_time_limit in the docs
-- edit--
The problem was in the for loop.
$i + 30 adds 30 to $i but it does not assign it.
It should be $i +=30

Related

PHP server side incremental counter

sorry I am new to PHP and need some help/guidance on creating a counter that will work server side, so I guess update an initial value?
I need for example to start with a base number of 1500 and have that number increase by 1 every 2 minutes, obviously so any visitors will see an increased number each time the visit.
Would the initial value need to be stored in sql or can a txt file be updated?
Any help would be great,
Thanks
It can be done in SQL if you want it but a text file is OK too, just save a value (1500), then create a cronjob and let it execute a PHP file where you'll have to set up the code that executes an SQL query which updates that value OR the code to update that text file every 2 minutes.
Example:
# Every two minutes
*/2 * * * * /your/path/too/this/file/updatecode.php
In your PHP file:
$SQL = "UPDATE table SET columnname = columname + 1";
// etc...
// OR the text file update code
If you don't need to store it specifically for some reason, then you don't need to run cron etc... Take a time stamp of a specific point in time you want to start at. Then calculate minutes since and add it to your start number (1500)
//Start Number
$n = 1500;
$cur_time = time();
$orig_time = strtotime("2013-10-21 10:00:00");
//New Number + difference in minutes (120 seconds for 2 mins) since start time
$newn = $n + round(abs($cur_time - $orig_time) / 120,0);
// Output New Number
echo $newn;
And if you wanted it in one line for copy/paste
echo 1500 + round(abs(time() - strtotime("2013-10-21 10:00:00")) / 120,0);
You could do this without a database just using dates. work out the difference in time between two dates (the current date and the starting date when you created the script), then divide that down into the correct amount of milliseconds for 2 minutes, and add that to your initial 1500.
If storing it is needed a SQL database for this is probably overkill.
Create you number, serialize it and store it to a file. Load it from the file next time, unserialize, increment, serialize and save.
You can save a time stamp along with the number to the file to avoid having to run some job every 2 minutes and instead calculate the correct value when you load the number back from the file.
Something like this (but error checking etc should be added and I haven't actually tried it to make sure the calculation is correct... but the main idea should be visible).
<?php
if(file_exists('mydatafile')) {
$data = unserialize(file_get_contents('mydatafile'));
// Calculate correct value based on time stamp
$data['number'] += round((time() - $data['timestamp']) / 120);
}
else {
// Init the number
$data["number"] = 1500;
}
// Print it if you need to here
// Update time stamp
$data["timestamp"] = time();
// Serialize and save data
file_put_contents('mydatafile', serialize($data)));

PHP: Maximum execution time of 30 seconds exceeded, continue looping

I'm writing a a php script where i do a loop and inside the loop there is a particular function processing information, as illustrated here.
while (some condition){
// some code
processInformation();
// Some more code
}
Well it turns out that midway, inside processInformation(), I encountered the error Fatal error: Maximum execution time of 30 seconds exceeded. Using set_time_limit(0), would not help in my situation because processInformation() might take forever. I don't want an error to completely break my program, so is there anyway i can catch an exception or tell php to "continue" the loop if processInformation() takes too long?
You can increase the value of maximum execution time from 30 seconds to any value. For example: if you want to set the limit of maximum execution time of 3 minutes (instead of 30 seconds), then you can set as:
ini_set('max_execution_time', 180); //maximum execution time of 3 minutes
This way, you can set the time based on your need.
I dunno how to catch the exception but you could use a simple time calculation to escape after say 60 secs. ()
$x=time();
while (some condition){
processInformation();
if ($x + 60) < time()
break;
}

random delay timer in PHP

I want to put a php daemon to sleep (with System_Daemon::iterate())
so it runs max 20 times randomly spread over an hour. maybe a min distance would be smart so it doesn't run 20 times in the first half hour and 0 times in the second half.
i'm kinda stuck here and don't know how to start with this one, any help is very apreciated!
You may use cron jobs, to set the script to run ever so often.
http://net.tutsplus.com/tutorials/php/managing-cron-jobs-with-php-2/
... Crontab:
0 9 * * * /path/to/bashscript
and in /path/to/bashscript:
#!/bin/bash
maxdelay=$((1*60)) # every hour, converted to minutes
for ((i=1; i<=20; i++)); do
delay=$(($RANDOM%maxdelay)) # pick an independent random delay, 20 times
(sleep $((delay*60)); /path/to/phpscript.php) & # background a subshell, then run the php script
done
i came up with one possible solution, i didnt try it out yet, so it main contain syntax or logic errors. because it is running as a daemon there is a never ending loop around it.
// 3600 seconds or one hour
$timeframe=3600;
// run max 20 times in $timeframe
$runtimes=20;
// minimum delay between two executions
$mindelay=60;
// maxium delay between two executions
$maxdelay=240;
if ($cnt % $runtimes != 0) {
$delay = rand($mindelay,$maxdelay);
System_Daemon::iterate($delay);
$sum += $delay;
$cnt++;
} else {
//final delay till the $timeframe
if ($sum < $timeframe) {
System_Daemon::iterate($timeframe - $sum);
}
$sum=0;
}
its not perfect and u waste some time but i guess its going to fullfill the job.
any comments?

Record live elapsed time of PHP script

Say my set_time_limit is set to 30 seconds and can't be changed. I have an email script in php that will run longer than 30 seconds on 1 cron job (I haven't reached that time mark yet.) What happens on timeout after 30 seconds? Will the script stop then continue?
If not, I would like to record the elapsed time from the beginning of my script's loop until it reaches 30 seconds and pause the process then continue.
What is a good way to do this?
Update: what I think might work
function email()
{
sleep(2); //delays script 2 seconds (time to break script on reset)
foreach ($emails as $email):
// send individual emails with different content
// takes longer than 30 seconds
enforeach;
// on 28 seconds
return email(); //restarts process
}
Suggested approach:
function microtime_float()
{
list($usec, $sec) = explode(" ", microtime());
return ((float)$usec + (float)$sec);
}
$time_start = microtime_float();
foreach ($emails as $email):
// send individual emails with different content
// takes longer than 30 seconds
$time_curr = microtime_float();
$time = $time_curr - $time_start;
if($time > 28){ //if time is bigger than 28 seconds (2 seconds less - just in case)
break;// we will continue processing the rest of the emails - next time cron will call the script
}
enforeach;
What you ask is not possible. When the script timeout is reached, it stops. You cannot pause it and continue it. You can only restart it. Let me tell you what I did to solve a similar problem.
Part One:
Queue all your emails into a simple database. (Just a few fields like to,
from, subject, message, and a flag to track if it is sent.)
Part Two.
1. Start script
2. Set a variable with the start time
3. Check the db for any mails that have not been sent yet
4. If you find any start a FOR loop
5. Check if more than 20 seconds have elapsed (adjust the window here, I like
to allow some spare time in case a mail call takes a few seconds longer.)
6. If too much time passed, exit the loop, go to step 10
7. Send an email
8. Mark this email as sent in the db
9. The FOR loop takes us back to step 4
10. Exit the script
Run this every 60 seconds. It sends what it can, then the rest gets sent next time.

Average sleep interval in PHP

I need to get average sleep interval from following data:
22:00-06:00
00:00-08:00
02:00-10:00
=> expected: 00:00-08:00
22:00-06:00
00:00-08:00
02:00-10:00
04:00-08:00
=> expected: 01:00-08:00
The problem is oscillation around midnight, where part is under 00:00 and part over 00:00.
Simple mean 22+00+02+04 doesn't work. I could count number of times over midnight (3 in this case) and if it's more than those before midnight, I should add 25 co compensate. But this doesn't count with those people, who work at night and go sleep around 8-14!
My theory is: First, somehow I need found peak, something like the most frequented area (e.g., in 9-10 there is 5 record, in 10-11, there is 3 etc.) and then I can decide co compensate border values by adding 24 hours.
What do you think?
What about taking into account relative difference with midnight ?
The result would be (-2+0+2+4)/4 = 00:45
Making the assumption that the person is not sleeping more than 24 hours, then make a method to calculate like so (pseudo code):
calculateSleepTime(sleepTime, wakeupTime) {
if (sleepTime > wakeupTime) {
return (24 - sleepTime) + wakeupTime;
} else {
return wakeupTime - sleepTime;
}
}
averageSleepTime(sleepTimesArray) {
totalSleptTime = 0;
totalTimesSlept = 0;
foreach (oneSleepTime in sleepTimesArray) {
totalTimesSlept++;
totalSleptTime += calculateSleepTime(oneSleepTime);
}
return totalSleptTime / totalTimesSlept;
}
After you get the average sleep time, calculate either the average sleep time, or average wake up time, and do addition/substraction to find your interval. The alternative to this is finding the average sleep time and the average wakeup time (taking relative to midnight times into account).
Determine a lower limit where you can say
Okay, people won't go to sleep this time of the day, if I get this time, it must mean they worked all night and only sleep now
and if the time is below that limit, add 24 hours to both start and finish.

Categories