PHP Date time plus minus program code _php tutorial

Source: Internet
Author: User
Tags difference between two times strcmp
Today we see in PHP to calculate the time difference between two times, the following we directly use the Data,strtotime and three functions are implemented, there is a need for friends to refer to.

This is the example that needs to be told today. Knowing a date and time,

Example: 2012-04-25 10:10:00

I want to add 5 months to this date and time and return to the date of processing.

Results: 2012-04-25 10:10:00 plus 5 months equals 2012-09-25 10:10:00


Combined with PHP function date () and Strtotime () two functions to achieve roughly this meaning,

The code is as follows Copy Code

/**
* Date Addition and subtraction method in PHP
* The old house of Qiong Tai
*/
The first step is to assume that there is a time
$a = ' 2012-04-25 10:10:00 ';

Step two, get the timestamp for this date
$a _time = strtotime ($a);

Step three, get the timestamp after five months plus
$b _time = Strtotime (' +5 Month ', $a _time);

Fourth, convert the timestamp back to the date format
$b = Date (' y-m-d h:i:s ', $b _time);
Echo ' This is a date added for five months '. $b;

If you think the above code is too long, you can do it one line.
$b = Date (' y-m-d h:i:s ', strtotime (' + '. $time. ' Month ', strtotime ($a)));
Echo ' This is a date added for five months '. $b;
?>

Common calculation time

code as follows copy code

Date_default_timezone_set (' PRC '); Default time zone
echo "Today:", Date ("Y-m-d", Time ()), "
";
echo "Today:", Date ("Y-m-d", Strtotime ("June 2008")), "
";
echo "Yesterday:", Date ("Y-m-d", Strtotime ("-1 Day")), "
";
echo "Tomorrow:", Date ("Y-m-d", Strtotime ("+1 Day"), "
";
echo "One week later:", Date ("Y-m-d", Strtotime ("+1 Week")), "
";
echo "2 days a week four hours two seconds later:", Date ("Y-m-d g:h:s", Strtotime ("+1 Week 2 Day 4 hours 2 Seconds")), "
";
echo "Next week Four:", Date ("Y-m-d", Strtotime ("next Thursday")), "
";
echo "Previous Monday:". Date ("Y-m-d", Strtotime ("Last Monday")). "
";
echo "One months ago:". Date ("Y-m-d", Strtotime ("Last month"). "
";
echo "One months later:". Date ("Y-m-d", Strtotime ("+1 Month")). "
";
echo "Ten years later:". Date ("Y-m-d", Strtotime ("+10 Year")). "
";
?>

Output results

Today: 2013-06-07
Today: 2008-06-18
Yesterday: 2013-06-06
Tomorrow: 2013-06-08
After one week: 2013-06-14
2 days a week four hours two seconds later: 2013-06-16 18:18:29
Next Thursday: 2013-06-13
Last Monday: 2013-06-03
One months ago: 2013-05-07
One months later: 2013-07-07
Ten years later: 2023-06-07

These look at some date plus and minus functions.

The code is as follows Copy Code

Get the day of the Week (1-7)
function Getweek ($times)
{
$res = Date (' W ', Strtotime ($times));
if ($res ==0)
$res = 7;
return $res;
}
Get the time of day
function GetTime ($times)
{
$res = Date (' H:i ', Strtotime ($times));
return $res;
}
Get the time in a few months now
function GetMonth ($Month, $type = ' l ')
{
if (!strcmp ($type, ' B '))
$res =date ("y-m-d h:i:s", Strtotime ("-$Month months"));
if (!strcmp ($type, ' l '))
$res =date ("y-m-d h:i:s", Strtotime ("+ $Month months"));
return $res;
}
Get current time
function GetCurrentDateTime ()
{
$res =date ("y-m-d h:i:s", Time ());
return $res;
}
Gets the time before or after the current time interval
function Getdiffhours ($hours, $type = ' l ')
{
if (!strcmp ($type, ' B '))
$res =date ("y-m-d h:i:s", Strtotime ("-$hours Hour"));
if (!strcmp ($type, ' l '))
$res =date ("y-m-d h:i:s", Strtotime ("+ $hours Hour"));
return $res;
}
Interval a few minutes before or after the time
function Getdiffminute ($Minute, $type = ' l ')
{
if (!strcmp ($type, ' B '))
$res =date ("y-m-d h:i:s", Strtotime ("-$Minute Minute"));
if (!strcmp ($type, ' l '))
$res =date ("y-m-d h:i:s", Strtotime ("+ $Minute Minute"));
return $res;
}
Interval a few seconds before or after the time
function Getdiffsec ($sec, $type = ' l ')
{
if (!strcmp ($type, ' B '))
$res =date ("y-m-d h:i:s", Strtotime ("-$sec Second"));
if (!strcmp ($type, ' l '))
$res =date ("y-m-d h:i:s", Strtotime ("+ $sec Second"));
return $res;
}

Interval a few weeks before or after the time
function Getdiffweek ($Week, $type = ' l ')
{
if (!strcmp ($type, ' B '))
$res =date ("y-m-d h:i:s", Strtotime ("-$Week Week"));
if (!strcmp ($type, ' l '))
$res =date ("y-m-d h:i:s", Strtotime ("+ $Week Week"));
return $res;
}
Time interval between days
function Getdiffdays ($days, $type = ' l ')
{
if (!strcmp ($type, ' B '))
$res =date ("y-m-d h:i:s", Strtotime ("-$days Day"));
if (!strcmp ($type, ' l '))
$res =date ("y-m-d h:i:s", Strtotime ("+ $days Day"));
return $res;
}
Time before or after a few years
function Getdiffyears ($year, $type = ' l ')
{
if (!strcmp ($type, ' B '))
$res =date ("y-m-d h:i:s", Strtotime ("-$year Year"));
if (!strcmp ($type, ' l '))
$res =date ("y-m-d h:i:s", Strtotime ("+ $year Year"));
return $res;
}

http://www.bkjia.com/PHPjc/631270.html www.bkjia.com true http://www.bkjia.com/PHPjc/631270.html techarticle today we see in PHP to calculate the time difference between two times, the following we directly use the Data,strtotime and three functions are implemented, there is a need for friends to refer to. Today I want to talk about ...

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