PHP date plus minus class, very good _php tips

Source: Internet
Author: User
How do you use this class? Take a look at the demo below:
Copy Code code as follows:

$temptime = time ();
Echo strftime ("%hh%m%A%d%b", $temptime);
$date = new Dateaccount ();
$temptime = $date->dateadd ("n", M, $temptime);
echo "<p>";
Echo strftime ("%hh%m%A%d%b", $temptime);

Copy Code code as follows:

$temptime = time ();
Echo strftime ("%hh%m%A%d%b", $temptime);
$date = new Dateaccount ();
$temptime = $date->dateadd ("n", M, $temptime);
echo "<p>";
Echo strftime ("%hh%m%A%d%b", $temptime);

If all goes well, you can see the following results:
15h41 Saturday June
16H31 Saturday June
Copy Code code as follows:

$currenttime = time ();
echo "Current time:". Strftime ("%hh%m%A%d%b", $currenttime). "<br>";
$date = new Dateaccount ();
$newtime = $date->dateadd ("n", M, $currenttime);
echo "Time plus minutes:". Strftime ("%hh%m%A%d%b", $newtime). "<br>";
$temptime = $date->datediff ("n", $currenttime, $newtime);
echo "Interval between two times:". $temptime;

Copy Code code as follows:

$currenttime = time ();
echo "Current time:". Strftime ("%hh%m%A%d%b", $currenttime). " <br> ";
$date = new Dateaccount ();
$newtime = $date->dateadd ("n", M, $currenttime);
echo "Time plus minutes:". Strftime ("%hh%m%A%d%b", $newtime). " <br> ";
$temptime = $date->datediff ("n", $currenttime, $newtime);
echo "Interval between two times:". $temptime;

If all goes well, you can see the following results:
Current TIME:16H23 Saturday June
Time plus minutes:17h13 Saturday June
Interval between two times:50
Copy Code code as follows:

<?php
Class dateaccount{
function __construct () {
}
function DateAdd ($interval, $number, $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;
}
function DateDiff ($interval, $date 1, $date 2) {
$timedifference = $date 2-$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;
}
}
?>

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.