-
Notifications
You must be signed in to change notification settings - Fork 73
Expand file tree
/
Copy pathStringUtil.php
More file actions
97 lines (80 loc) · 2.3 KB
/
StringUtil.php
File metadata and controls
97 lines (80 loc) · 2.3 KB
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
88
89
90
91
92
93
94
95
96
97
<?php
/**
* StringUtil
* PHP version 5
*
* @category Class
* @package XeroAPI\XeroPHP
* @author Xero API team
* @link
*/
namespace XeroAPI\XeroPHP;
class StringUtil
{
/**
* Constructor
*/
public function __construct()
{
}
/**
* Converts String to Date
*
* @param string $data string im MS DateFormat "/Date(321494400000+0000)/"
*
* @return $this
*/
public static function convertStringToDate($data)
{
if( self::checkThisDate($data) ) {
return new \DateTime($data);
} else {
// convert Microsoft .NET JSON Date format into native PHP DateTime()
preg_match('/Date\(([-]?\d+)([+,-]?\d*)\)/', $data, $matches);
$dateTime = \DateTime::createFromFormat(
'U.u',
sprintf("%d.%06d", $matches[1] / 1000, ($matches[1] % 1000) * 1000)
);
return $dateTime->setTimezone(new \DateTimeZone($matches[2]));
}
}
public static function convertStringToDateTime($data)
{
if( self::checkThisDate($data) ) {
return new \DateTime($data);
} else {
// Data not in a format that simply converts to a new DateTime();
// Custom Date Deserializer to allow for Xero's use of .NET JSON Date format
$match = preg_match( '/([\d]{11})/', $data, $date );
if($match){
$seconds = $date[1]/1000;
}
$match = preg_match( '/([\d]{12})/', $data, $date );
if($match){
$seconds = $date[1]/1000;
}
$match = preg_match( '/([\d]{13})/', $data, $date );
if($match){
$seconds = $date[1]/1000;
}
$datetime = new \DateTime();
$datetime->setTimestamp($seconds);
$result = $datetime->format('Y-m-d H:i:s');
$date = new \DateTime($result);
return $date;
}
}
public static function checkThisDate($value)
{
if (!$value) {
return false;
}
try {
new \DateTime($value);
return true;
} catch (\Exception $e) {
return false;
}
}
}
?>