Time Processing in PHP
PHP Introductory Training tutorial Brother even PHP training
Small compilation of Time processing in PHP:
<?
/**
* Convert to UNIX timestamp
*/
function GetTime ($d) {
if (Is_numeric ($d))
return $d;
else {
if (! is_string ($d)) Return0;
if (Ereg (":", $d)) {
$buf = Split ("+", $d);
$year =split ("[-/]", $buf [0]);
$hour =split (":", $buf [1]);
if (Eregi ("PM", $buf [2]))
$hour [0] + = 12;
Returnmktime ($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, $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); 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;
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 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<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 there is also ". DateDiff (" s ", $d 3," 2002/2/1212:59:59 ")." Seconds <br> ";
?>
PHP Introductory Training Tutorial time processing in PHP