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
<?php

$d1 = new DateTime('2010-06-17 14:00:00', new DateTimeZone('America/Montreal'));
$d2 = new DateTime('2010-06-17 14:00:00', new DateTimeZone('America/Santiago'));

echo "D1: " . $d1->getTimezone()->getName() . "\n";
echo "D2: " . $d2->getTimezone()->getName() . "\n";
// D1: America/Montreal
// D2: America/Santiago

echo "D1 Format: " . $d1->format('Y-m-d H:i:sO') . "\n";
echo "D2 Format: " . $d2->format('Y-m-d H:i:sO') . "\n";
// D1 Format: 2010-06-17 14:00:00-0400
// D2 Format: 2010-06-17 14:00:00-0400

$d3 = DateTime::createFromFormat('Y-m-d H:i:sO', '2010-06-17 14:00:00-0400');
echo "D3: " . $d3->getTimezone()->getName() . "\n";
echo "D3 Format: " . $d3->format('Y-m-d H:i:sO') . "\n";
// D3: -04:00
// D3: 2010-06-17 14:00:00-0400

$d1->modify('+5 month');
$d2->modify('+5 month');
$d3->modify('+5 month');

echo "D1 Format: " . $d1->format('Y-m-d H:i:sO') . "\n";
echo "D2 Format: " . $d2->format('Y-m-d H:i:sO') . "\n";
echo "D3 Format: " . $d3->format('Y-m-d H:i:sO') . "\n";
// D1 Format: 2010-11-17 14:00:00-0500
// D2 Format: 2010-11-17 14:00:00-0300
// D3 Format: 2010-11-17 14:00:00-0400

// this following code may be implementable using @PrePersist/@PreUpdate and @PostLoad
$d4 = DateTime::createFromFormat('Y-m-d H:i:s', '2010-06-17 14:00:00');
$d4->setTimeZone(new DateTimeZone('America/Montreal'));
$d5 = DateTime::createFromFormat('Y-m-d H:i:s', '2010-06-17 14:00:00');
$d5->setTimeZone(new DateTimeZone('America/Santiago'));

$d4->modify('+5 month');
$d5->modify('+5 month');

echo "D4 Format: " . $d4->format('Y-m-d H:i:sO') . "\n";
echo "D5 Format: " . $d5->format('Y-m-d H:i:sO') . "\n";

// yields wrong results however
// D4 Format: 2010-11-17 08:00:00-0500
// D5 Format: 2010-11-17 08:00:00-0300

// This is the correct way, cannot be implemented currently however in a decent way
$d6 = DateTime::createFromFormat('Y-m-d H:i:s', '2010-06-17 14:00:00', new DateTimeZone('America/Montreal'));
$d7 = DateTime::createFromFormat('Y-m-d H:i:s', '2010-06-17 14:00:00', new DateTimeZone('America/Santiago'));

$d6->modify('+5 month');
$d7->modify('+5 month');

echo "D6 Format: " . $d6->format('Y-m-d H:i:sO') . "\n";
echo "D7 Format: " . $d7->format('Y-m-d H:i:sO') . "\n";

// correct results:
// D6 Format: 2010-11-17 14:00:00-0500
// D7 Format: 2010-11-17 14:00:00-0300