Hi there

I've written the basics of a PHP driven calendar that allows you to create a recurring event. For example, I attempted to generate an event every Thursday for 3 months, to do this I call the PHP function "strtotime" (http://php.net/manual/en/function.strtotime.php) numerous times.

However, when I attempt this, I get a 500 Internal Server Error, and my error logs are reporting the following;

[Mon Aug 20 02:04:09 2007] [error] [client <<IP>>] FATAL: emalloc(): Unable to allocate 35 bytes
[Mon Aug 20 02:04:09 2007] [error] [client <<IP>>] Premature end of script headers: php4

The code in question is as follows;

	$recur = array();
	$week = array('Monday','Tuesday','Wednesday','Thursday','Friday','Saturday','Sunday');
	$start = strtotime($_POST['date_year'].'-'.$_POST['date_month'].'-'.$_POST['date_day']);
	$until = strtotime($_POST['until_year'].'-'.$_POST['until_month'].'-01');
	$curr = $start;
	while($curr<$until) {
		$recur[] = $curr;
		$curr = strtotime("next ".$week[$_POST['frequency_day']],$curr);
	}

Or, with the POST's filled out for testing purposes;

<?php
	// test emalloc bug
	$recur = array();
	$week = array('Monday','Tuesday','Wednesday','Thursday','Friday','Saturday','Sunday');
	$start = strtotime('2007-09-07');
	$until = strtotime('2007-12-01');
	$curr = $start;
	while($curr<$until) {
		$recur[] = $curr;
		$curr = strtotime("next ".$week[3],$curr);
	}
	print_r($recur);
?>

Surely there should be no issues calling a pretty stock standard PHP function several times - I mean for every Thursday for 3 months we're only talking around 15-20 times right? Thoughts?

Cheers

James

    I have used strtotime() in a similar manner without any such error (PHP 5.2.1, Apache 2.2.4 (Win32) running on Windows Vista). Perhaps it might be useful to add a little debug output in the loop, to see if it's immediately crashing, or if it's chewing up memory until it dies:

    <?php
        // test emalloc bug
        $recur = array();
        $week = array('Monday','Tuesday','Wednesday','Thursday','Friday','Saturday','Sunday');
        $start = strtotime('2007-09-07');
        $until = strtotime('2007-12-01');
        $curr = $start;
        while($curr<$until) {
            echo 'Memory usage: ' . memory_get_usage() . "<br />\n";
            $recur[] = $curr;
            $curr = strtotime("next ".$week[3],$curr);
        }
        print_r($recur);
    ?>
    

      Thanks for that - yeah I'll try it, but I'm thinking it isn't PHP with the memory issue - considering I get thrown a 500 Server Error message o_O

        Write a Reply...