xref: /PHP-7.3/ext/date/tests/date_diff.phpt (revision 9f8501b3)
1--TEST--
2Extensive test for date_diff().
3--SKIPIF--
4<?php
5if (getenv("SKIP_SLOW_TESTS")) die("skip slow test");
6?>
7--INI--
8date.timezone=UTC
9--FILE--
10<?php
11$ok = 0;
12define( 'COUNT', 120 );
13$d0 = new DateTime('2009-11-20');
14for ( $i = 0; $i < COUNT * 12; $i++ )
15{
16	$d = clone $d0;
17	$dates[$i] = $d->add( new DateInterval( "P{$i}D" ) );
18}
19
20for ( $i = 0; $i < COUNT; $i++)
21{
22//	echo $dates[$i]->format( "Y-m-d\n" );
23	for ( $j = 0; $j < COUNT * 12; $j++)
24	{
25		$diff = date_diff( $dates[$i], $dates[$j] );
26		/*
27		printf( "\t%s %s %3d %s\n",
28			$dates[$i]->format( 'Y-m-d' ),
29			$dates[$j]->format( 'Y-m-d' ),
30			$diff->format( '%a' ),
31			$diff->format( '%y-%m-%d' )
32		);
33		*/
34
35		$current = clone $dates[$i];
36		$int = new DateInterval( $diff->format( 'P%yY%mM%dD' ) );
37		if ( $current > $dates[$j] )
38		{
39			$current->sub( $int );
40		}
41		else
42		{
43			$current->add( $int );
44		}
45		if ( $current != $dates[$j] )
46		{
47			echo "FAIL: ",
48				$dates[$i]->format( 'Y-m-d' ), " + ",
49				$int->format( '%y-%m-%d' ), " = ",
50				$current->format( 'Y-m-d' ), " (",
51				$dates[$j]->format( 'Y-m-d' ), ")\n";
52		}
53		else
54		{
55			$ok++;
56		}
57	}
58}
59
60echo $ok, "\n";
61?>
62--EXPECT--
63172800
64