1--TEST--
2Fractions with DateTime objects
3--INI--
4date.timezone=UTC
5--FILE--
6<?php
7/* This will go wrong, once in a million times */
8$ms = date_create()->format('u');
9echo ($ms = 0) ? "microseconds = false\n" : "microseconds = true\n";
10
11/* Normal creation */
12echo date_create( "2016-10-03 12:47:18.819313" )->format( "Y-m-d H:i:s.u" ), "\n\n";
13
14/* With modifications */
15$dt = new DateTimeImmutable( "2016-10-03 12:47:18.819210" );
16echo $dt->modify( "+1 day" )->format( "Y-m-d H:i:s.u" ), "\n";
17
18$dt = new DateTimeImmutable( "2016-10-03 12:47:18.081921" );
19echo $dt->modify( "-3 months" )->format( "Y-m-d H:i:s.u" ), "\n";
20
21echo "\n";
22
23/* These should reset the time (and hence fraction) to 0 */
24$dt = new DateTimeImmutable( "2016-10-03 12:47:18.081921" );
25echo $dt->modify( "yesterday" )->format( "Y-m-d H:i:s.u" ), "\n";
26
27$dt = new DateTimeImmutable( "2016-10-03 12:47:18.081921" );
28echo $dt->modify( "noon" )->format( "Y-m-d H:i:s.u" ), "\n";
29
30$dt = new DateTimeImmutable( "2016-10-03 12:47:18.081921" );
31echo $dt->modify( "10 weekday" )->format( "Y-m-d H:i:s.u" ), "\n";
32
33/* Interval containing fractions */
34
35$dt1 = new DateTimeImmutable( "2016-10-03 13:20:07.103123" );
36$dt2 = new DateTimeImmutable( "2016-10-03 13:20:07.481312" );
37$diff = $dt1->diff( $dt2 );
38
39var_dump( $diff );
40
41$dt0 = $dt1->sub( $diff );
42$dt3 = $dt2->add( $diff );
43$dt4 = $dt3->add( $diff );
44
45echo $dt0->format( "Y-m-d H:i:s.u" ), "\n";
46echo $dt1->format( "Y-m-d H:i:s.u" ), "\n";
47echo $dt2->format( "Y-m-d H:i:s.u" ), "\n";
48echo $dt3->format( "Y-m-d H:i:s.u" ), "\n";
49echo $dt4->format( "Y-m-d H:i:s.u" ), "\n";
50?>
51--EXPECTF--
52microseconds = true
532016-10-03 12:47:18.819313
54
552016-10-04 12:47:18.819210
562016-07-03 12:47:18.081921
57
582016-10-02 00:00:00.000000
592016-10-03 12:00:00.000000
602016-10-17 12:47:18.081921
61object(DateInterval)#%d (16) {
62  ["y"]=>
63  int(0)
64  ["m"]=>
65  int(0)
66  ["d"]=>
67  int(0)
68  ["h"]=>
69  int(0)
70  ["i"]=>
71  int(0)
72  ["s"]=>
73  int(0)
74  ["f"]=>
75  float(0.378189)
76  ["weekday"]=>
77  int(0)
78  ["weekday_behavior"]=>
79  int(0)
80  ["first_last_day_of"]=>
81  int(0)
82  ["invert"]=>
83  int(0)
84  ["days"]=>
85  int(0)
86  ["special_type"]=>
87  int(0)
88  ["special_amount"]=>
89  int(0)
90  ["have_weekday_relative"]=>
91  int(0)
92  ["have_special_relative"]=>
93  int(0)
94}
952016-10-03 13:20:06.724934
962016-10-03 13:20:07.103123
972016-10-03 13:20:07.481312
982016-10-03 13:20:07.859501
992016-10-03 13:20:08.237690
100