-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDateFormat.php
121 lines (107 loc) · 2.37 KB
/
DateFormat.php
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
<?php
/**
* Copyright 2016 Engin Halaç <[email protected]>
*
* @author Engin Halaç <[email protected]>
* @url https://github.com/enqinhlc/php-date-format-changer
* @version 1.0
*/
class DateFormatChanger {
/**
* @var string
*/
private $date = 'd/m/y';
/**
* @var string
*/
private $format = 'd/m/y';
/**
* @var string
*/
private $seperator = '/';
/**
* @var string
*/
private $returnFormat = 'ymd';
/**
* @var string
*/
private $returnSeperator = '-';
/**
* @var bool
*/
private $isUnix = false;
/**
* @param string $date
*/
function setDate($date = 'd/m/y') {
date_default_timezone_set('Europe/Istanbul');
$this->date = $date;
}
/**
* @param string $format
*/
function setFormat($format = 'd/m/y') {
$this->format = $format;
}
/**
* @param string $seperator
*/
function setSeperator($seperator = '/') {
$this->seperator = $seperator;
}
/**
* @param string $returnFormat
*/
function setReturnFormat($returnFormat = 'ymd') {
$this->returnFormat = $returnFormat;
}
/**
* @param string $returnSeperator
*/
function setReturnSeperator($returnSeperator = '-') {
$this->returnSeperator = $returnSeperator;
}
/**
* @param bool $isUnix
*/
function isUnix($isUnix = false) {
$this->isUnix = $isUnix;
}
/**
* @return int|string
*/
function getDate() {
$parse = explode($this->seperator, strtolower($this->format));
$regex = array();
foreach ($parse as $key => $value) {
$regex[] = '(?<' . $value . '>\d+)';
}
preg_match('#' . implode('/', $regex) . '#', $this->date, $m);
$returnParse = array();
for ($i = 0; $i < strlen($this->returnFormat); $i++) {
$formatParam = $this->returnFormat{$i};
if (isset($m[ strtolower($formatParam) ])) {
$returnParse[] = $m[ strtolower($formatParam) ];
} else {
$returnParse[] = date($formatParam);
}
}
$newDate = implode($this->returnSeperator, $returnParse);
if ($this->isUnix === false) {
return $newDate;
} else {
return mktime(0, 0, 0, $m['m'], $m['d'], $m['y']);
}
}
}
# $dfc = new DateFormatChanger();
# $dfc->setDate('11/08/2016');
# $dfc->setFormat('d/m/y'); // default
# $dfc->setSeperator('/'); // default
# $dfc->setReturnFormat('dmy');
# $dfc->setReturnSeperator('-'); // default
# echo $dfc->getDate() . "\n"; // 11-08-2016
# $dfc->setReturnSeperator('/');
# echo $dfc->getDate() . "\n"; // 11/08/2016
?>