Date Special Operation Control class

Source: Internet
Author: User
Tags add days date current time
Control

?
Date Special Operation Control class
Yufan August 15
?>
?
Gets the current time for the "YYYY-MM-DD" format
function Nowdate ()
{
$result = Date ("Y"). " -". Date (" M ")." -". Date (" D ");
return $result;
}

Gets the current time for the "YYYY-MM-DD HH:ii:ss" format
function Nowdatetime ()
{
$result = Date ("Y"). " -". Date (" M ")." -". Date (" D ")." ". Date (" H ").": ". Date (" I ").": ". Date (" S ");
return $result;
}

 //extract year from the YYYY-MM-DD format
 function getyear ($Date)
 {
  $ pos = Strpos ($Date, "-");
  $result = "";
  for ($i =0 $i < $pos $i + +)
  {
   $result = $result. $Date [$i];
 }
  $month = getmonth ($Date);
  $day = Getday ($Date);
  if (!checkdate ($month, $day, $result)
  {
   $result = false;
 }
  return $result;
 }

 //extract months from the YYYY-MM-DD format
 function getmonth ($Date)
 {
  $ POS1 = Strpos ($Date, "-");
  $pos 2 = Strrpos ($Date, "-");
  $result = "";
  for ($i = $pos 1+1 $i < $pos 2; $i + +)
  {
   $result = $result. $Date [$i];
 }
  $year = getyear ($Date);
  $day = Getday ($Date);
  if (!checkdate ($result, $day, $year)
  {
   $result = false;
 }
  return $result;
 }

Extract day from the time in "YYYY-MM-DD" format
function Getday ($Date)
{
$pos = Strrpos ($Date, "-");
$result = "";
for ($i = $pos +1; $i <10; $i + +)
{
$result = $result. $Date [$i];
}
$year = getyear ($Date);
$month = Getday ($Date);
if (!checkdate ($month, $result, $year))
{
$result = false;
}
return $result;
return $result;
}

Check leap year, return Boolean value
function Checkleap ($year)
{
$result = ($year% 4 = 0) && (($year% <> 0) | | ($year% 400 = 0)));
return $result;
}

Add days on the current date to return to the "YYYY-MM-DD" format
function Addday ($offset)
{
$year =date ("Y");
$month =date ("M");
$day =date ("D");
for ($i =0; $i < $offset; $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;
}

Time to return the "YYYY-MM-DD" format on the current date
function Abateday ($offset)
{
$year =date ("Y");
$month =date ("M");
$day =date ("D");
for ($i =0; $i < $offset; $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;
}

Gets the time on the first day of the current week to return to the "YYYY-MM-DD" format
function Firstdayofthisweek ()
{
$offset =date ("w");
if ($offset ==0)
{
$offset = 7;
}
$offset--;
$result = Abateday ($offset);
return $result;
}

Gets the time when the last day of the current week returns to the "YYYY-MM-DD" format
function Lastdayofthisweek ()
{
$offset =date ("w");
if ($offset ==0)
{
$offset = 7;
}
$offset = 7-$offset;
$result = Addday ($offset);
return $result;
}

Gets the number of days for a specified month
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;
}

?>




Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.