MkFramework
 All Data Structures Functions
plugin_datetime.php
1 <?php
2 /*
3 This file is part of Mkframework.
4 
5 Mkframework is free software: you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as published by
7 the Free Software Foundation, either version 3 of the License.
8 
9 Mkframework is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU Lesser General Public License for more details.
13 
14 You should have received a copy of the GNU Lesser General Public License
15 along with Mkframework. If not, see <http://www.gnu.org/licenses/>.
16 
17 */
24 
25  protected $iHour;
26  protected $iMinute;
27  protected $iSecond;
28 
35  public function __construct($sDate=null,$sFormat='Y-m-d h:i:s'){
36  if($sDate!=null){
37  if(!$this->loadFromFormat($sDate,$sFormat)){
38  return false;
39  }
40  }else{
41  $this->reset();
42  }
43  }
50  public function loadFromFormat($sDate,$sFormat){
51  list($sYear,$sMonth,$sDay,$sHour,$sMinute,$sSecond)=$this->convertFromFormatToTab($sDate,$sFormat);
52  $this->setYear((int)$sYear);
53  $this->setMonth((int)$sMonth);
54  $this->setDay((int)$sDay);
55  $this->setHour((int)$sHour);
56  $this->setMinute((int)$sMinute);
57  $this->setSecond((int)$sSecond);
58 
59  if(!$this->isValid() ){
60  $this->reset();
61  return false;
62  }
63 
64  return true;
65  }
69  public function reset(){
70  $this->iAnnee=null;
71  $this->iMois=null;
72  $this->iJour=null;
73  $this->iJoursemaine=null;
74 
75  $this->iHeure=null;
76  $this->iMinute=null;
77  $this->iSeconde=null;
78  }
79 
84  public function getHour(){
85  return sprintf('%02d',$this->iHour);
86  }
91  public function getMinute(){
92  return sprintf('%02d',$this->iMinute);
93  }
98  public function getSecond(){
99  return sprintf('%02d',$this->iSecond);
100  }
101 
106  public function setHour($iHour){
107  $this->iHour=(int)$iHour;
108  }
113  public function setMinute($iMinute){
114  $this->iMinute=(int)$iMinute;
115  }
120  public function setSecond($iSecond){
121  $this->iSecond=(int)$iSecond;
122  }
123 
129  public function toString($sFormat='Y-m-d H:i:s'){
130  return date($sFormat,$this->getMkTime());
131  }
132 
137  public function getMkTime(){
138  return mktime($this->iHour,$this->iMinute,$this->iSecond,$this->iMonth,$this->iDay,$this->iYear);
139  }
140 
141  private function convertFromFormatToTab($sDate,$sFormat){
142 
143  $iHeure=0;
144  $iMinute=0;
145  $iSeconde=0;
146 
147  if($sFormat=='Y-m-d'){
148  list($iAnnee,$iMois,$iJour)=explode('-',$sDate);
149  }elseif($sFormat=='d-m-Y'){
150  list($iJour,$iMois,$iAnnee)=explode('-',$sDate);
151  }elseif($sFormat=='d/m/Y'){
152  list($iJour,$iMois,$iAnnee)=explode('/',$sDate);
153  }elseif($sFormat=='Y/m/d'){
154  list($iAnnee,$iMois,$iJour)=explode('/',$sDate);
155  }elseif($sFormat=='m-d-Y'){
156  list($iMois,$iJour,$iAnnee)=explode('-',$sDate);
157  }elseif($sFormat=='y-m-d'){
158  list($iAnnee,$iMois,$iJour)=explode('-',$sDate);
159  $iAnnee=2000+intval($iAnnee);
160  }elseif($sFormat=='Y-m-d h:i:s'){
161  $tDatetime=preg_split('/\s/',$sDate);
162  list($iAnnee,$iMois,$iJour)=explode('-',$tDatetime[0]);
163  list($iHeure,$iMinute,$iSeconde)=explode(':',$tDatetime[1]);
164  }
165 
166 
167  return array(
168  sprintf('%04d',$iAnnee),
169  sprintf('%02d',$iMois),
170  sprintf('%02d',$iJour),
171  sprintf('%02d',$iHeure),
172  sprintf('%02d',$iMinute),
173  sprintf('%02d',$iSeconde),
174  );
175 
176  }
177 }
__construct($sDate=null, $sFormat='Y-m-d h:i:s')
setMonth($iMonth)
Definition: plugin_date.php:84
loadFromFormat($sDate, $sFormat)
setYear($iYear)
Definition: plugin_date.php:77
setDay($iDay)
Definition: plugin_date.php:91
toString($sFormat='Y-m-d H:i:s')