WPDK  1.5.0
WordPress Development Kit
 All Data Structures Files Functions Variables Pages
wpdk-datetime.php
Go to the documentation of this file.
1 <?php
14 class WPDKDateTime extends WPDKObject {
15 
21  const MYSQL_DATE = 'Y-m-d';
22 
28  const MYSQL_DATE_TIME = 'Y-m-d H:i:s';
29 
37  public $__version = '1.0.2';
38 
50  public static function format( $date, $to = 'm/d/Y H:i' )
51  {
52  $result = $date;
53  if ( !empty( $date ) ) {
54  $timestamp = strtotime( $date );
55  $result = date( $to, $timestamp );
56  }
57  return $result;
58  }
59 
73  static function expirationDate( $date, $duration, $duration_type )
74  {
75  $expiredate = strtotime( "+{$duration} {$duration_type}", strtotime( $date ) );
76  return $expiredate;
77  }
78 
88  static function daysToDate( $date )
89  {
90  $diff = $date - time();
91  $days = floatval( round( $diff / ( 60 * 60 * 24 ) ) );
92  return $days;
93  }
94 
102  static function timeNewLine( $datetime )
103  {
104  return str_replace( ' ', '<br/>', $datetime );
105  }
106 
116  static function isExpired( $exipration )
117  {
118  return ( ( $exipration - time() ) <= 0 );
119  }
120 
133  public static function isInRangeDatetime( $date_start, $date_expire, $format = 'YmdHis', $timestamp = false )
134  {
135  if ( !empty( $date_start ) || !empty( $date_expire ) ) {
136 
137  /* Get now in timestamp */
138  $now = mktime();
139 
140  /* Le date sono in chiaro o anch'esse in timestamp? */
141  /* @todo qui si potrebbe provare a capire in automatico se la data รจ in timestamp o stringa, ad esempio usando is_numeric() */
142  if ( !$timestamp ) {
143  $date_start = !empty( $date_start ) ? strtotime( $date_start ) : $now;
144  $date_expire = !empty( $date_expire ) ? strtotime( $$date_expire ) : $now;
145  }
146  else {
147  $date_start = !empty( $date_start ) ? $date_start : $now;
148  $date_expire = !empty( $date_expire ) ? $date_expire : $now;
149  }
150 
151  /* Verifico il range. */
152  if ( $now >= $date_start && $now <= $date_expire ) {
153  return true;
154  }
155  }
156  return false;
157  }
158 
169  public static function mySQLDate( $date )
170  {
171  return self::format( $date, self::MYSQL_DATE );
172  }
173 
184  public static function mySQLDateTime( $datetime )
185  {
186  return self::format( $datetime, self::MYSQL_DATE_TIME );
187  }
188 
196  public static function stripSecondsFromTime( $time )
197  {
198  return substr( $time, 0, 5 ); // 00:00
199  }
200 
213  public static function daysToWeekStart( $date, $first_day = 'monday' )
214  {
215 
216  $week_days = array(
217  'monday' => 0,
218  'tuesday' => 1,
219  'wednesday' => 2,
220  'thursday' => 3,
221  'friday' => 4,
222  'saturday' => 5,
223  'sunday' => 6
224 
225  );
226 
227  $start_day_number = $week_days[$first_day];
228  $wday = $date->format( "w" );
229  $current_day_number = ( $wday != 0 ? $wday - 1 : 6 );
230  return WPDKMath::rModulus( ( $current_day_number - $start_day_number ), 7 );
231 
232  }
233 
244  public static function beginningOfWeek( $date, $first_day = 'monday' )
245  {
246  $days_to_start = WPDKDateTime::daysToWeekStart( $date, $first_day );
247  return ( $date - $days_to_start );
248  }
249 
250  // -----------------------------------------------------------------------------------------------------------------
251  // TODOS
252  // -----------------------------------------------------------------------------------------------------------------
253 
254 
256  public static function compareDate()
257  {
258  }
259 
261  public static function compareDatetime()
262  {
263  }
264 
265 
266  // -----------------------------------------------------------------------------------------------------------------
267  // DEPRECATED
268  // -----------------------------------------------------------------------------------------------------------------
269 
285  public static function formatFromFormat( $date, $deprecated = '', $to = 'm/d/Y H:i' )
286  {
287  if ( !empty( $deprecated ) && func_num_args() > 2 ) {
288  _deprecated_argument( __METHOD__, '1.0.0.b2' );
289  }
290 
291  _deprecated_function( __METHOD__, '1.0.0', 'format()' );
292 
293  return self::format( $date, $to );
294  }
295 
307  public static function makeTimeFrom( $format, $date )
308  {
309 
310  _deprecated_function( __METHOD__, '1.0.0.b2', 'strtotime() PHP function' );
311 
312  /* Get date in m/d/Y H:i */
313  $sanitize_date = self::formatFromFormat( $date, $format );
314  $split = explode( ' ', $sanitize_date );
315  $date_part = explode( '/', $split[0] );
316  $time_part = explode( ':', $split[1] );
317  $time = mktime( $time_part[0], $time_part[1], 0, $date_part[0], $date_part[1], $date_part[2] );
318  return $time;
319  }
320 
332  public static function date2MySql( $date, $deprecated = '' )
333  {
334  if ( !empty( $deprecated ) ) {
335  _deprecated_argument( __METHOD__, '1.0.0.b2' );
336  }
337  _deprecated_function( __CLASS__ . '::' . __FUNCTION__, '1.3.0', 'mySQLDate()' );
338  return self::mySQLDate( $date );
339  }
340 
353  public static function dateTime2MySql( $datetime, $deprecated = '' )
354  {
355  if ( !empty( $deprecated ) ) {
356  _deprecated_argument( __METHOD__, '1.0.0.b2' );
357  }
358  _deprecated_function( __CLASS__ . '::' . __FUNCTION__, '1.3.0', 'mySQLDateTime()' );
359  return self::mySQLDateTime( $datetime );
360  }
361 
362 
363 }