PHP date formatting and date calculation and when getting before week, month and end date
PHP date format Sample code:
/ **
* Formatting time
* $ type: type
* $ strDate: time string to be processed
*
* Year Y: four-digit year y: two-digit year
* Month m: two-digit month n: one-digit month M: English month
* Date d: Two-digit date j: One-digit date D: English date
* Hour: H, minute: i, second: s
** /
public function GetFormatDate ($ type = 1, $ strDate = '') {
$ time = time ();
if (isset ($ strDate) &&! empty ($ strDate)) {
$ time = strtotime ($ strDate);
}
switch ($ type) {
case 1:
return date ("H: i", $ time);
case 2:
return date ("m month d H: i", $ time);
case 3:
return date ("m / d H: i", $ time);
case 4:
return date ("Y: m, d, H: i", $ time);
case 5:
return date ("Y / m / d H: i", $ time);
case 6:
return date ("H: i: s", $ time)
case 7:
return date ("Y-m-d H: i: s", $ time);
case 8:
return date ("Y / m / d H: i: s", $ time);
default:
return $ strDate;
}
}
Date Calculation Sample code:
/** *
time plus minus processing
* $strDate: Time string to be processed
* $days: Add and Subtract days
**/public
function ChangeDate ($strDate, $ Days) {
$time = time ();
if (Isset ($strDate) &&!empty ($strDate)) {
$time = Strtotime ($strDate);
}
Return date (' y-m-d h:i:s ', Strtotime ("$days Day", $time));
}
Get the current week, the month-and-tail date sample code:
/ **
* Get the beginning and end of the current week and month
*
* $ dateArr ['W1']: Monday
* $ dateArr ['W7']: Weekend
* $ dateArr ['M1']: month head
* $ dateArr ['M2']: end of the month
** /
public function GetCurrentDateInfo () {
$ dayTimes = 24 * 60 * 60;
$ dateArr = []; $ temp = '';
$ weekIndex = (int) date ('w');
switch ($ weekIndex) {
case 0:
$ dateArr ['W1'] = date ('Y-m-d 00: 00: 00', strtotime ('+ 1 day'));
$ dateArr ['W7'] = date ('Y-m-d 23: 59: 59', strtotime ('+ 7 day'));
break;
case 1:
$ dateArr ['W1'] = date ('Y-m-d 00:00:00');
$ dateArr ['W7'] = date ('Y-m-d 23: 59: 59', strtotime ('+ 6 day'));
break;
case 2:
$ dateArr ['W1'] = date ('Y-m-d 00: 00: 00', strtotime ('-1 day'));
$ dateArr ['W7'] = date ('Y-m-d 23: 59: 59', strtotime ('+ 5 day'));
break;
case 3:
$ dateArr ['W1'] = date ('Y-m-d 00: 00: 00', strtotime ('-2 day'));
$ dateArr ['W7'] = date ('Y-m-d 23: 59: 59', strtotime ('+ 4 day'));
break;
case 4:
$ dateArr ['W1'] = date ('Y-m-d 00: 00: 00', strtotime ('-3 day'));
$ dateArr ['W7'] = date ('Y-m-d 23: 59: 59', strtotime ('+ 3 day'));
break;
case 5:
$ dateArr ['W1'] = date ('Y-m-d 00: 00: 00', strtotime ('-4 day'));
$ dateArr ['W7'] = date ('Y-m-d 23: 59: 59', strtotime ('+ 2 day'));
break;
case 6:
$ dateArr ['W1'] = date ('Y-m-d 00: 00: 00', strtotime ('-5 day'));
$ dateArr ['W7'] = date ('Y-m-d 23: 59: 59', strtotime ('+ 1 day'));
break;
}
// 1-12: January to December
$ monthIndex = (int) date ('m');
switch ($ monthIndex) {
case 1:
$ temp = date ('Y-02-01 00:00:00');
$ dateArr ['M1'] = date ('Y-m-01 00:00:00');
$ dateArr ['M2'] = date ('Y-m-d 23: 59: 59', strtotime ($ temp)-$ dayTimes);
break;
case 2:
$ temp = date ('Y-03-01 00:00:00');
$ dateArr ['M1'] = date ('Y-m-01 00:00:00');
$ dateArr ['M2'] = date ('Y-m-d 23: 59: 59', strtotime ($ temp)-$ dayTimes);
break;
case 3:
$ temp = date ('Y-04-01 00:00:00');
$ dateArr ['M1'] = date ('Y-m-01 00:00:00');
$ dateArr ['M2'] = date ('Y-m-d 23: 59: 59', strtotime ($ temp)-$ dayTimes);
break;
case 4:
$ temp = date ('Y-05-01 00:00:00');
$ dateArr ['M1'] = date ('Y-m-01 00:00:00');
$ dateArr ['M2'] = date ('Y-m-d 23: 59: 59', strtotime ($ temp)-$ dayTimes);
break;
case 5:
$ temp = date ('Y-06-01 00:00:00');
$ dateArr ['M1'] = date ('Y-m-01 00:00:00');
$ dateArr ['M2'] = date ('Y-m-d 23: 59: 59', strtotime ($ temp)-$ dayTimes);
break;
case 6:
$ temp = date ('Y-07-01 00:00:00');
$ dateArr ['M1'] = date ('Y-m-01 00:00:00');
$ dateArr ['M2'] = date ('Y-m-d 23: 59: 59', strtotime ($ temp)-$ dayTimes);
break;
case 7:
$ temp = date ('Y-08-01 00:00:00');
$ dateArr ['M1'] = date ('Y-m-01 00:00:00');
$ dateArr ['M2'] = date ('Y-m-d 23: 59: 59', strtotime ($ temp)-$ dayTimes);
break;
case 8:
$ temp = date ('Y-09-01 00:00:00');
$ dateArr ['M1'] = date ('Y-m-01 00:00:00');
$ dateArr ['M2'] = date ('Y-m-d 23: 59: 59', strtotime ($ temp)-$ dayTimes);
break;
case 9:
$ temp = date ('Y-10-01 00:00:00');
$ dateArr ['M1'] = date ('Y-m-01 00:00:00');
$ dateArr ['M2'] = date ('Y-m-d 23: 59: 59', strtotime ($ temp)-$ dayTimes);
break;
case 10:
$ temp = date ('Y-11-01 00:00:00');
$ dateArr ['M1'] = date ('Y-m-01 00:00:00');
$ dateArr ['M2'] = date ('Y-m-d 23: 59: 59', strtotime ($ temp)-$ dayTimes);
break;
case 11:
$ temp = date ('Y-12-01 00:00:00');
$ dateArr ['M1'] = date ('Y-m-01 00:00:00');
$ dateArr ['M2'] = date ('Y-m-d 23: 59: 59', strtotime ($ temp)-$ dayTimes);
break;
case 12:
$ temp = date ((date ('Y') + 1). "-01-01 00:00:00");
$ dateArr ['M1'] = date ('Y-m-01 00:00:00');
$ dateArr ['M2'] = date ('Y-m-d 23: 59: 59', strtotime ($ temp)-$ dayTimes);
break;
}
return $ dateArr;
}
The above code is for reference only, the place of omission also please point out to improve!