peachpiecompiler / peachpie

PeachPie - the PHP compiler and runtime for .NET and .NET Core
https://www.peachpie.io
Apache License 2.0
2.31k stars 201 forks source link

Date operations not working as expected #1053

Open StefanduPlessis opened 2 years ago

StefanduPlessis commented 2 years ago

Please let me know if I should rather split the following issues into separate issues, but for convenience's sake I'll keep them in this one.

1. DatePeriod

In PHP you were able to use iterator_to_array on a DatePeriod object, but this is not possible at the moment, because it throws an ArgumentException. From looking at the source code, I suspect its because it extends Traversable and not IteratorAggregate, as it use to in PHP.

My next step was just to try to iterate through the DatePeriod object, but this did not resolve my issue. Example:

$fromDate = new DateTime("2022-01-01");
$toDate = new DateTime("2022-01-05");
$interval = DateInterval::createFromDateString("+1 Day");
$datePeriod = new DatePeriod($fromDate, $interval, $toDate);
foreach($datePeriod as $day){
  echo $day->format("Y-m-d")." ";
}

Expected:

2022-01-01 2022-01-02 2022-01-03 2022-01-04 2022-01-05

Result

2020-01-01

I saw that it is stated on the Compatibility status page of the project that DatePeriod::getIterator() is not yet supported, which I assume is the root of my issue. Will this be done in the near future or should I rather invest time into alternative solutions?

2. DateTime->modify uses wrong date

When modifying a DateTime object the date is reset to the current time instead of using the initialized value. Example:

$date = (new DateTime("2022-01-01"))->modify("+1 month");
echo $date->format("Y-m-d");

Expected:

2022-02-01

Result:

2022-06-25

When using the modify function's parameters in the DateTime object's constructor you get the correct result (new DateTime("2022-01-01 +1 month"); \\ 2022-02-01)

3. DateTime->modify using complex strings

This one is not too important. In PHP you use to be able to use 'complex' strings to modify dates. For example say you wanted the first day of the month you were able to use $date->modify('first day of month');, but it throws Pchp.Library.Spl.Exception: 'Parse error on position 3 near 'st day of this month' currently.

4. DateInterval::createFromDateString ignores negative signs

PHP handles negative values in the createFromDateString function correctly, by setting the day, month or year to negative (and not invert for some reason). Currently peachpie does neither. PHP:

$interval = DateInterval::createFromDateString('-1 day');
echo var_dump($interval);
object(DateInterval)[12]
  public 'y' => int 0
  public 'm' => int 0
  public 'd' => int -1
  public 'h' => int 0
  public 'i' => int 0
  public 's' => int 0
  public 'f' => float 0
  public 'weekday' => int 0
  public 'weekday_behavior' => int 0
  public 'first_last_day_of' => int 0
  public 'invert' => int 0
  public 'days' => boolean false
  public 'special_type' => int 0
  public 'special_amount' => int 0
  public 'have_weekday_relative' => int 0
  public 'have_special_relative' => int 0

Peachpie:

$interval = DateInterval::createFromDateString('-1 day');
echo var_dump($interval);
class DateInterval#56168358 (9) {
["y"]=> int(0) 
["m"]=> int(0) 
["invert"]=> int(0) 
["d"]=> int(1) 
["h"]=> int(0) 
["i"]=> int(0) 
["s"]=> int(0) 
["f"]=> double(0) 
["days"]=> bool(false) }