Time Processing in PHP

Source: Internet
Author: User
/**
* Convert to UNIX timestamp
*/
function GetTime ($d) {
if (Is_numeric ($d))
return $d;
else {
if (! is_string ($d)) return 0;
if (Ereg (":", $d)) {
$buf = Split ("+", $d);
$year = Split ("[-/]", $buf [0]);
$hour = Split (":", $buf [1]);
if (Eregi ("PM", $buf [2]))
$hour [0] + = 12;
return mktime ($hour [0], $hour [1], $hour [2], $year [1], $year [2], $year [0]);
}else {
$year = Split ("[-/]", $d);
Return Mktime (0,0,0, $year [1], $year [2], $year [0]);
}
}
}

/**
*
* DATEADD (Interval,number,date)
* Returns the date when the specified time interval has been added.
* Inetrval is a string expression that represents the interval to be added, such as minutes or days
* number is a numeric expression that represents the number of time intervals to be added
* Date indicates dates
*
* Interval (time interval string expression) can be any of the following values:
* YYYY year
* Q Quarter Quarter
* M Month months
* Y Day of the Year
* D Day
* W weekday days of the week
* WW Week of the year week
* H Hour hours
* N minute min
* S Second sec
* W, y and D are exactly the same, i.e. add one day to the current date, Q plus 3 months, WW plus 7 days.
*/
function DateAdd ($interval,, $date) {
$date = gettime ($date);
$date _time_array = getdate ($date);
$hours = $date _time_array["Hours"];
$minutes = $date _time_array["Minutes"];
$seconds = $date _time_array["seconds"];
$month = $date _time_array["Mon"];
$day = $date _time_array["Mday"];
$year = $date _time_array["year"];
Switch ($interval) {
Case "yyyy": $year + =; Break
Case "Q": $month + = (); Break
Case "M": $month + =; Break
Case "Y":
Case "D":
Case "W": $day + =; Break
Case "WW": $day + = (*7); Break
Case "H": $hours + =; Break
Case "n": $minutes + =; Break
Case "s": $seconds + =; Break
}
$timestamp = Mktime ($hours, $minutes, $seconds, $month, $day, $year);
return $timestamp;
}

/**
* DateDiff (INTERVAL,DATE1,DATE2)
* Returns a time interval between two dates
* Intervals (time interval string expression) can be any of the following values:
* W Week
* D Day
* H hours
* N Minutes
* s seconds
*/
function DateDiff ($interval, $date 1, $date 2) {
Get the number of seconds between two dates
$timedifference = gettime ($date 2)-gettime ($date 1);
Switch ($interval) {
Case "W": $retval = Bcdiv ($timedifference, 604800); Break
Case "D": $retval = Bcdiv ($timedifference, 86400); Break
Case "h": $retval = Bcdiv ($timedifference, 3600); Break
Case "n": $retval = Bcdiv ($timedifference, 60); Break
Case "s": $retval = $timedifference; Break
}
return $retval;
}

?>



Test example
$d 1 = "2002-01-11";
$d 2 = Date ("y-m-d", DateAdd ("D", 1, $d));

echo $d 1. " of ". DateDiff ("D", $d 1, $d 2). " Tin Hau is $d2
";
echo $d 1. " 10 days ago was ". Date (" Y-m-d ", DateAdd (" D ", -10, $d 1))."
";
$d 3 = Date ("y/m/d h:i:s");
echo "is now". $d 3. " From 2002/2/12 12:59:59 there is also ". DateDiff (" s ", $d 3," 2002/2/12 12:59:59 ")." Seconds
";

?>


  • 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.