1--TEST-- 2Test DateInterval::__serialize and DateInterval::__unserialize 3--FILE-- 4<?php 5date_default_timezone_set("Europe/London"); 6 7$d = DateInterval::createFromDateString('next weekday'); 8echo "Original object:\n"; 9var_dump($d); 10 11echo "\n\nSerialised object:\n"; 12$s = serialize($d); 13var_dump($s); 14 15echo "\n\nUnserialised object:\n"; 16$e = unserialize($s); 17var_dump($e); 18 19echo "\n\nCalling __serialize manually:\n"; 20var_dump($d->__serialize()); 21 22echo "\n\nCalling __unserialize manually:\n"; 23$d = new DateInterval('P2Y4DT6H8M'); 24$d->__unserialize( 25 [ 26 'from_string' => true, 27 'date_string' => 'next weekday', 28 ] 29); 30var_dump($d); 31 32echo "\n\nUsed serialised interval:\n"; 33$now = new DateTimeImmutable("2022-04-22 16:25:11 BST"); 34var_dump($now->add($e)); 35var_dump($now->sub($e)); 36?> 37--EXPECTF-- 38Original object: 39object(DateInterval)#1 (%d) { 40 ["from_string"]=> 41 bool(true) 42 ["date_string"]=> 43 string(%d) "next weekday" 44} 45 46 47Serialised object: 48string(%d) "O:12:"DateInterval":2:{s:11:"from_string";b:1;s:11:"date_string";s:%d:"next weekday";}" 49 50 51Unserialised object: 52object(DateInterval)#2 (2) { 53 ["from_string"]=> 54 bool(true) 55 ["date_string"]=> 56 string(%d) "next weekday" 57} 58 59 60Calling __serialize manually: 61array(2) { 62 ["from_string"]=> 63 bool(true) 64 ["date_string"]=> 65 string(%d) "next weekday" 66} 67 68 69Calling __unserialize manually: 70object(DateInterval)#3 (2) { 71 ["from_string"]=> 72 bool(true) 73 ["date_string"]=> 74 string(%d) "next weekday" 75} 76 77 78Used serialised interval: 79object(DateTimeImmutable)#4 (3) { 80 ["date"]=> 81 string(26) "2022-04-25 16:25:11.000000" 82 ["timezone_type"]=> 83 int(2) 84 ["timezone"]=> 85 string(3) "BST" 86} 87 88Warning: DateTimeImmutable::sub(): Only non-special relative time specifications are supported for subtraction in %s on line %d 89object(DateTimeImmutable)#4 (3) { 90 ["date"]=> 91 string(26) "2022-04-22 16:25:11.000000" 92 ["timezone_type"]=> 93 int(2) 94 ["timezone"]=> 95 string(3) "BST" 96} 97