控制
//日期特殊操作控制类
//宇凡 8月15日
?>
//获取"YYYY-mm-dd"格式的当前时间
function NowDate()
{
$result = date("Y")."-".date("m")."-".date("d");
return $result;
}
//获取"YYYY-mm-dd HH:ii:ss"格式的当前时间
function NowDateTime()
{
$result = date("Y")."-".date("m")."-".date("d")." ".date("H").":".date("i").":".date("s");
return $result;
}
//从"YYYY-mm-dd"格式的时间中提取年
function GetYear($Date)
{
$pos = strpos($Date,"-");
$result = "";
for ($i=0;$i {
$result = $result.$Date[$i];
}
$month = GetMonth($Date);
$day = GetDay($Date);
if (!checkdate($month,$day,$result))
{
$result = false;
}
return $result;
}
//从"YYYY-mm-dd"格式的时间中提取月
function GetMonth($Date)
{
$pos1 = strpos($Date,"-");
$pos2 = strrpos($Date,"-");
$result = "";
for ($i=$pos1+1;$i {
$result = $result.$Date[$i];
}
$year = GetYear($Date);
$day = GetDay($Date);
if (!checkdate($result,$day,$year))
{
$result = false;
}
return $result;
}
//从"YYYY-mm-dd"格式的时间中提取日
function GetDay($Date)
{
$pos = strrpos($Date,"-");
$result = "";
for ($i=$pos+1;$i {
$result = $result.$Date[$i];
}
$year = GetYear($Date);
$month = GetDay($Date);
if (!checkdate($month,$result,$year))
{
$result = false;
}
return $result;
return $result;
}
//检查闰年,返回布尔值
function CheckLeap($year)
{
$result = (($year % 4 == 0) && (($year % 100 0) || ($year % 400 == 0)));
return $result;
}
//在当前日期上加天数 返回"YYYY-mm-dd"格式的时间
function AddDay($offset)
{
$year=date("Y");
$month=date("m");
$day=date("d");
for ($i=0;$i {
switch ($month) {
case 1:
{
if ($day==31) {
$day = 1;
$month = 2;
}
else {
$day=$day + 1;
}
}
break;
case 2:
{
if (CheckLeap($year)) {
if ($day == 29) {
$day = 1;
$month = 3;
}
else {
$day=$day + 1;
}
}
else {
if ($day == 28) {
$day = 1;
$month = 3;
}
else {
$day=$day + 1;
}
}
}
break;
case 3:
{
if ($day==31) {
$day = 1;
$month = 4;
}
else {
$day=$day + 1;
}
}
break;
case 4:
{
if ($day==30) {
$day = 1;
$month = 5;
}
else {
$day=$day + 1;
}
}
break;
case 5:
{
if ($day==31) {
$day = 1;
$month = 6;
}
else {
$day=$day + 1;
}
}
break;
case 6:
{
if ($day==30) {
$day = 1;
$month = 7;
}
else {
$day=$day + 1;
}
}
break;
case 7:
{
if ($day==31) {
$day = 1;
$month = 8;
}
else {
$day=$day + 1;
}
}
break;
case 8:
{
if ($day==31) {
$day = 1;
$month = 9;
}
else {
$day=$day + 1;
}
}
break;
case 9:
{
if ($day==30) {
$day = 1;
$month = 10;
}
else {
$day=$day + 1;
}
}
break;
case 10:
{
if ($day==31) {
$day = 1;
$month = 11;
}
else {
$day=$day + 1;
}
}
break;
case 11:
{
if ($day==30) {
$day = 1;
$month = 12;
}
else {
$day=$day + 1;
}
}
break;
case 12:
{
if ($day==31) {
$day = 1;
$month = 1;
$year = $year + 1;
}
else {
$day=$day + 1;
}
}
}
}
$result=$year."-".$month."-".$day;
return $result;
}
//在当前日期上减天数 返回"YYYY-mm-dd"格式的时间
function AbateDay($offset)
{
$year=date("Y");
$month=date("m");
$day=date("d");
for ($i=0;$i {
switch ($month) {
case 1:
{
if ($day==1) {
$day = 31;
$month = 12;
$year = $year - 1;
}
else {
$day=$day - 1;
}
}
break;
case 2:
{
if ($day == 1) {
$day = 31;
$month = 1;
}
else {
$day=$day - 1;
}
}
break;
case 3:
{
if ($day == 1) {
if (CheckLeap($year)) {
$day = 29;
}
else {
$day = 28;
}
$month = 2;
}
else {
$day=$day - 1;
}
}
break;
case 4:
{
if ($day == 1) {
$day = 31;
$month = 3;
}
else {
$day=$day - 1;
}
}
break;
case 5:
{
if ($day == 1) {
$day = 30;
$month = 4;
}
else {
$day=$day - 1;
}
}
break;
case 6:
{
if ($day == 1) {
$day = 31;
$month = 5;
}
else {
$day=$day - 1;
}
}
break;
case 7:
{
if ($day == 1) {
$day = 30;
$month = 6;
}
else {
$day=$day - 1;
}
}
break;
case 8:
{
if ($day == 1) {
$day = 31;
$month = 7;
}
else {
$day=$day - 1;
}
}
break;
case 9:
{
if ($day == 1) {
$day = 31;
$month = 8;
}
else {
$day=$day - 1;
}
}
break;
case 10:
{
if ($day == 1) {
$day = 30;
$month = 9;
}
else {
$day=$day - 1;
}
}
break;
case 11:
{
if ($day == 1) {
$day = 31;
$month = 10;
}
else {
$day=$day - 1;
}
}
break;
case 12:
{
if ($day == 1) {
$day = 30;
$month = 11;
}
else {
$day=$day - 1;
}
}
}
}
$result=$year."-".$month."-".$day;
return $result;
}
//获取当前周第一天 返回"YYYY-mm-dd"格式的时间
function FirstDayOfThisWeek()
{
$offset=date("w");
if ($offset==0)
{
$offset=7;
}
$offset--;
$result = AbateDay($offset);
return $result;
}
//获取当前周最后一天 返回"YYYY-mm-dd"格式的时间
function LastDayOfThisWeek()
{
$offset=date("w");
if ($offset==0)
{
$offset=7;
}
$offset = 7 - $offset;
$result = AddDay($offset);
return $result;
}
//获取指定月的天数
function DaysAmountOfThisMonth($month,$year)
{
switch ($month) {
case 1:
$result = 31;
break;
case 2:
{
if (CheckLeap($year)) {
$result = 29;
} else {
$result = 28;
}
break;
}
case 3:
$result = 31;
break;
case 4:
$result = 30;
break;
case 5:
$result = 31;
break;
case 6:
$result = 30;
break;
case 7:
$result = 31;
break;
case 8:
$result = 31;
break;
case 9:
$result = 30;
break;
case 10:
$result = 31;
break;
case 11:
$result = 30;
break;
case 12:
$result = 31;
break;
}
return $result;
}
?>