Home > OFFER, PHP, php function, Technology, web services, Wordpress > Other some php function in Date & month calculation

Other some php function in Date & month calculation

function dateFormat($strDate,$strFormat) {
if($strDate==””)
$strDate = date(“Y-m-d H:i:s”);
switch($strFormat) {
case 1:
return date(“Y-m-d”,strtotime($strDate));
break;
case 2:
return date(“Y-m-d H:i:s”,strtotime($strDate));
break;
case 3:
return date(“F j,Y”,strtotime($strDate));
break;
case 4:
return date(“F j,Y H:i:s”,strtotime($strDate));
break;
case 5:
return date(“F j,Y H:i”,strtotime($strDate));
break;
case 6:
return date(“m/d/Y”,strtotime($strDate));
break;
case 7:
$myDate = explode(” “,$strDate);
$strDateList = explode(“-“,$myDate[0]);
$DDate   = $strDateList[2];
$DMonth  = $strDateList[1];
$DYear   = $strDateList[0];
return $DDate.”/”.$DMonth.”/”.$DYear;

//return date(“d/m/Y”,strtotime($strDate));
break;
case 8:
$myDate = explode(” “,$strDate);
$strDateList = explode(“-“,$myDate[0]);
$DDate   = $strDateList[2];
$DMonth  = $strDateList[1];
$DYear   = $strDateList[0];
return $DDate.”-“.$DMonth.”-“.$DYear;

//return date(“d/m/Y”,strtotime($strDate));
break;
case 9:
return date(“F Y”,strtotime($strDate));
break;
case 10:
return date(“d/m/Y”,strtotime($strDate));
break;
case 11:
return date(“l”,strtotime($strDate));
break;
case 12:
$myDate = explode(” “,$strDate);
$strDateList = explode(“-“,$myDate[0]);
$DDate   = $strDateList[1];
$DMonth  = $strDateList[0];
$DYear   = $strDateList[2];
return $DYear.”-“.$DMonth.”-“.$DDate;

break;
}
}

function getdateDiff($myDate1, $myDate2){
// Initial declarations for clarity.
list($monFrom, $dayFrom, $yearFrom) = $myDate1;
list($monTo, $dayTo, $yearTo) = $myDate2;

// Is $dateFrom before $dateTo?
$swap = 0;
if($yearFrom > $yearTo || ($yearFrom == $yearTo && ($monFrom > $monTo || ($monFrom == $monTo && $dayFrom > $dayTo))))
{
// Swap the dates.
$swap = 1;
list($myDate1, $myDate2) = array($myDate2, $myDate1);
list($monFrom, $dayFrom, $yearFrom) = $myDate1;
list($monTo, $dayTo, $yearTo) = $myDate2;
}

// How many years (leap and non-leap) occur during the timeframe?
$fullYears = $yearTo – $yearFrom – 1;

// How many leap years occur during the timeframe?
$fullLeapYears = getFullLeapYears($yearFrom, $yearTo);

// Subtract the number of fullLeapYears from the number of fullYears.
// $fullYears = the number of non-leap years during the timeframe.
// $fullLeapYears = the number of leap years during the timeframe.
$fullYears -= $fullLeapYears;

// Calculate the number of months left over.
if($yearFrom != $yearTo)
{
// Count the number of months between $monFrom and $monTo.
if($monFrom < $monTo)
{
$fullMonths = $monTo – $monFrom – 1;
}
// Comepensate for the 12 month wrap-around.
else
{
// This is the same thing as 12 – ($monFrom – $monTo) – 1.
$fullMonths = 12 – $monFrom + $monTo – 1;
}

// Calculate the number of days left over.
$fullDays = 0;

// Count up the number of days in each month from $monFrom + 1 up to December.
for($i = $monFrom + 1; $i <= 12; $i++)
{
$fullDays += getDaysInMonth(array($i, 1, $yearFrom));
}

// Count up the number of days in each month from January to $monTo – 1.
for($i = 1; $i < $monTo; $i++)
{
$fullDays += getDaysInMonth(array($i, 1, $yearTo));
}

// Add the leftover days between $dayFrom and $dayTo.
$fullDays += getDaysInMonth(array($monFrom, 1, $yearFrom)) – $dayFrom + $dayTo;
}
// $yearFrom and $yearTo are the same.
else
{
$fullDays = 0;

// Add up the number of days in each month between $monFrom and $monTo (not inclusively).
for($i = $monFrom + 1; $i < $monTo; $i++)
{
$fullDays += getDaysInMonth(array($i, 1, $yearTo));
}

// If we are looking at two dates in the same month (of the same year).
if($monFrom == $monTo)
{
// Simply subtract $dayFrom from $dayTo.
$fullDays += $dayTo – $dayFrom;
}
// The months are different.
else
{
// Add the leftover days between $dayFrom and $dayTo.
$fullDays += getDaysInMonth(array($monFrom, 1, $yearFrom)) – $dayFrom + $dayTo;
}
}

// If $fullYears comes out to be -1 (which happens when they are equal) add one.
$fullYears += ($fullYears == -1);

// Finally, we need to determine if the timespan contains the break where
// the Gregorian calendar was established.
$gregorianSkip = 0;

if(
(
($yearFrom == 1582 && $monFrom == 10 && $dayFrom <= 4) ||
($yearFrom == 1582 && $monFrom < 10) ||
($yearFrom < 1582)
) &&
(
($yearTo == 1582 && $monTo == 10 && $dayTo >= 15) ||
($yearTo == 1582 && $monTo > 10) ||
($yearTo > 1582)
)
)
{
$gregorianSkip = 10;
}
// The return value is a little tricky.
// The number of days is 365 * years + 366 * leapYears + fullDays.
// If we swapped the dates originally, throw in a negative sign.
return pow(-1, $swap) * (365 * $fullYears + 366 * $fullLeapYears + $fullDays – $gregorianSkip);
}

function getFullLeapYears($yearFrom, $yearTo){
if($yearFrom == $yearTo) // The following calculations only need to be made if the years are different.
{
return 0;
} else {
// Find the first leap year after $yearFrom.
for($i = $yearFrom + 1; !isLeapYear($i); $i++);
// Find the first leap year before $yearTo.
for($j = $yearTo – 1; !isLeapYear($j); $j–);
// Find the difference between the two leap years.
$diff = $j – $i;
// Divide the difference by 4, throw out all the 100-years and add back all the 400-years.
return $diff / 4 – floor($diff / 100) + floor($diff / 400) + 1;
}
}

function isLeapYear($year){
$leapYear = 0;
if($year != 4) // Year 4 was not a leap year!
{
if(!($year % 4)) // if $year is divisible by 4
{
$leapYear = 1;
if(!($year % 100) && ($year % 400)) // if $year is divisible by 100 and not divisible by 400
{
$leapYear = 0;
}
}
}
return $leapYear;
}

function getDaysInMonth($myDate){
// Initial declarations for clarity.
list($month, $day, $year) = $myDate;
// Look at the $month variable
switch($month){
// February (Accounts for leap years)
case 2 :
$days = 28 + isLeapYear($year);
break;
// April, June, September, November
case 4 :
case 6 :
case 9 :
case 11 :
$days = 30;
break;
// January, March, May, July, August, October, December
default :
$days = 31;
}
return $days;
}

  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: