Bug->Req #79104 [Asn]: Relative DateInterval Creates Infinite Loop

From: Date: Tue, 06 Apr 2021 19:23:51 +0000
Subject: Bug->Req #79104 [Asn]: Relative DateInterval Creates Infinite Loop
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-233246@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=79104&edit=1

 ID:                 79104
 Updated by:         derick@php.net
 Reported by:        jimbo2150 at gmail dot com
 Summary:            Relative DateInterval Creates Infinite Loop
 Status:             Assigned
-Type:               Bug
+Type:               Feature/Change Request
 Package:            Date/time related
 PHP Version:        7.3.13
 Assigned To:        derick
 Block user comment: N
 Private report:     N

 New Comment:

I'm not sure whether it is possible to do this with a one-shot advancement yet.


Previous Comments:
------------------------------------------------------------------------
[2021-02-03 17:24:34] cmb@php.net

> but the result is erratic (not sure what is being calculated)

The algorithm selects the first Monday of January of the *current*
year, and then advances 1 year.  Not sure, how to categorize that.

------------------------------------------------------------------------
[2020-01-24 12:15:35] antonino dot spampinato86 at gmail dot com

observe the behavior of DateTime, this code expands the relative date for the current year while for
DatePeriod it must not expand for the current year but must use the value for the start and end
year, it means if I have a year explicitly expressed DatePeriod result must be for the year
expressed otherwise if the year is not expressed it recovers the start and end of DatePeriod.
$var = new DateTime( 'first monday of January', new DateTimeZone( "UTC" );
$display[5] = $var->format( "l Y-m-d H:i:s" );
var_dump( $display );

------------------------------------------------------------------------
[2020-01-23 23:52:17] antonino dot spampinato86 at gmail dot com

$timezone = new DateTimeZone( "UTC" );
$fmj = DateInterval::createFromDateString( 'first Monday of January 2020' );
//Monday from start 2019 only
$period = new DatePeriod( new DateTime( '2019-01-01 00:00:00.000000', $timezone ), $fmj,
new DateTime( '2022-01-11 00:00:00.000000', $timezone ) );

$y = 0;
foreach ( $period as $dt ) {

    $display[$y++] = $dt->format( "l Y-m-d H:i:s" );
		//stop total_recurrence
		if( $y == 5 ) {
			break;
		}
}
Manually stop infinite loop :)
The relative date, first Monday of jenaury is extended for the current year but for the parametres
start and end of function DatePeriod , only uses the start, there are two bugs in the php source
code, the reference is not counted if the code does not return the expected result, the second bug
is stopping on start.

------------------------------------------------------------------------
[2020-01-23 21:41:55] jimbo2150 at gmail dot com

Not sure what you mean by the numbers you are looking for.

I got closer with this code (added "next year" to the DatePeriod object, but the result is
erratic (not sure what is being calculated):


$fmj = DateInterval::createFromDateString('first Monday of January next year');

$period = new DatePeriod(new DateTime('first Monday of January 2020'), $fmj, new
DateTime('2022-01-31'));

foreach ($period as $dt) {
    var_dump($dt->format("l Y-m-d H:i:s"));
}

// Strange, incorrect output (not the first Monday of January 2021 or 2022):
string(26) "Monday 2020-01-06 00:00:00"
string(29) "Wednesday 2021-01-06 00:00:00"
string(27) "Tuesday 2022-01-04 00:00:00"

------------------------------------------------------------------------
[2020-01-19 11:04:57] a at b dot c dot de

Looks like it can't determine how long "first Monday of January" is.

echo $fmj->format('%y years, %m months, %d days, %h hours, %i minutes, %s seconds');

What numbers should it be using for those fields?

------------------------------------------------------------------------


The remainder of the comments for this report are too long. To view
the rest of the comments, please view the bug report online at

    https://bugs.php.net/bug.php?id=79104


--
Edit this bug report at https://bugs.php.net/bug.php?id=79104&edit=1


Thread (10 messages)

« previous php.bugs (#233246) next »