Time Processing in PHP

Source: Internet
Author: User
Tags date expression split time interval
?
/**
* 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 A string expression representing the time interval to add, such as minute or day
* number is a numeric expression that represents the number of time intervals to add
* Date indicates
*
* Interval (time interval string expression) can be any of the following values:
* YYYY year
* Q Quarter Quarter
* M Month Month
* Y days of year
* D Day
* W weekday days in a week
* WW Week of Year Week
* H Hour hours
* N minute points
* s second seconds
* The functions of W, Y and D are exactly the same, that is, add a day to the current date, Q plus 3 months, WW plus 7 days.
*/
function DateAdd ($interval, $number, $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 + + $number; Break
Case "Q": $month + + ($number *3); Break
Case "M": $month + + $number; Break
Case "Y":
Case "D":
Case "W": $day + + $number; Break
Case "WW": $day + + ($number *7); Break
Case "H": $hours + + $number; Break
Case "n": $minutes + + $number; Break
Case "s": $seconds + + $number; Break
}
$timestamp = Mktime ($hours, $minutes, $seconds, $month, $day, $year);
return $timestamp;
}

/**
* DateDiff (INTERVAL,DATE1,DATE2)
* Returns the 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) {
To 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", $d 1));

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

?>


Related Article

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.