File: date_time_fractions.phpt

package info (click to toggle)
php8.4 8.4.16-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 211,276 kB
  • sloc: ansic: 1,176,142; php: 35,419; sh: 11,964; cpp: 7,208; pascal: 4,951; javascript: 3,091; asm: 2,817; yacc: 2,411; makefile: 696; xml: 446; python: 301; awk: 148
file content (87 lines) | stat: -rw-r--r-- 2,175 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
--TEST--
Fractions with DateTime objects
--INI--
date.timezone=UTC
--FILE--
<?php
/* This will go wrong, once in a million times */
$ms = date_create()->format('u');
echo ($ms = 0) ? "microseconds = false\n" : "microseconds = true\n";

/* Normal creation */
echo date_create( "2016-10-03 12:47:18.819313" )->format( "Y-m-d H:i:s.u" ), "\n\n";

/* With modifications */
$dt = new DateTimeImmutable( "2016-10-03 12:47:18.819210" );
echo $dt->modify( "+1 day" )->format( "Y-m-d H:i:s.u" ), "\n";

$dt = new DateTimeImmutable( "2016-10-03 12:47:18.081921" );
echo $dt->modify( "-3 months" )->format( "Y-m-d H:i:s.u" ), "\n";

echo "\n";

/* These should reset the time (and hence fraction) to 0 */
$dt = new DateTimeImmutable( "2016-10-03 12:47:18.081921" );
echo $dt->modify( "yesterday" )->format( "Y-m-d H:i:s.u" ), "\n";

$dt = new DateTimeImmutable( "2016-10-03 12:47:18.081921" );
echo $dt->modify( "noon" )->format( "Y-m-d H:i:s.u" ), "\n";

$dt = new DateTimeImmutable( "2016-10-03 12:47:18.081921" );
echo $dt->modify( "10 weekday" )->format( "Y-m-d H:i:s.u" ), "\n";

/* Interval containing fractions */

$dt1 = new DateTimeImmutable( "2016-10-03 13:20:07.103123" );
$dt2 = new DateTimeImmutable( "2016-10-03 13:20:07.481312" );
$diff = $dt1->diff( $dt2 );

var_dump( $diff );

$dt0 = $dt1->sub( $diff );
$dt3 = $dt2->add( $diff );
$dt4 = $dt3->add( $diff );

echo $dt0->format( "Y-m-d H:i:s.u" ), "\n";
echo $dt1->format( "Y-m-d H:i:s.u" ), "\n";
echo $dt2->format( "Y-m-d H:i:s.u" ), "\n";
echo $dt3->format( "Y-m-d H:i:s.u" ), "\n";
echo $dt4->format( "Y-m-d H:i:s.u" ), "\n";
?>
--EXPECTF--
microseconds = true
2016-10-03 12:47:18.819313

2016-10-04 12:47:18.819210
2016-07-03 12:47:18.081921

2016-10-02 00:00:00.000000
2016-10-03 12:00:00.000000
2016-10-17 12:47:18.081921
object(DateInterval)#%d (%d) {
  ["y"]=>
  int(0)
  ["m"]=>
  int(0)
  ["d"]=>
  int(0)
  ["h"]=>
  int(0)
  ["i"]=>
  int(0)
  ["s"]=>
  int(0)
  ["f"]=>
  float(0.378189)
  ["invert"]=>
  int(0)
  ["days"]=>
  int(0)
  ["from_string"]=>
  bool(false)
}
2016-10-03 13:20:06.724934
2016-10-03 13:20:07.103123
2016-10-03 13:20:07.481312
2016-10-03 13:20:07.859501
2016-10-03 13:20:08.237690