PHP Output time difference function Code _php skill

Source: Internet
Author: User

PHP Output Time difference function

Copy Code code as follows:

<?php 
Date_default_timezone_set (' PRC ');//default time zone  
echo "Today:", Date ("Y-m-d", Time ()), "<br>"; 
Echo "Today:", Date ("Y-m-d", Strtotime ("June 2008")), "<br > "; 
Echo yesterday:", Date ("Y-m-d", Strtotime ("-1 Day"), "<br>"; 
Echo "Tomorrow:", Date ("y-m-d", Strtotime ("+1 Day"), "<br>"; 
Echo after one week: ", Date (" Y-m-d ", Strtotime (" +1 Week "))," <br> "; 
echo "2 days a week four hours two seconds later:", Date ("Y-m-d g:h:s", Strtotime ("+1 Week 2 days 4 hours 2 Seconds"), "<br>"; 
Echo "Next Thursday: ", Date (" Y-m-d ", Strtotime (" next Thursday "))," <br> "; 
Echo" Last Monday: ". Date (" Y-m-d ", Strtotime (" Previous Monday "))." <br> "; 
Echo one months ago:". Date ("Y-m-d", Strtotime ("Last month"). " <br> "; 
Echo one months later:". Date ("Y-m-d", Strtotime ("+1 Month")). " <br> "; 
Echo ten years later:". Date ("Y-m-d", Strtotime ("+10 Year")). " <br> "; 
?>

When learning PHP, it is often used to get the date before or after a certain time period. The collection is now in place, and you can also expand the rich
Copy Code code as follows:

Get the day of the Week (1-7)
function Getweek ($times)
{
$res = Date (' W ', Strtotime ($times));
if ($res ==0)
$res = 7;
return $res;
}
Get Day time
function GetTime ($times)
{
$res = Date (' H:i ', Strtotime ($times));
return $res;
}
Get the time of the month 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;
}
Time before or after a few minutes interval
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;
}
Time before or after a few seconds interval
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;
}

A few weeks before or after the interval.
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;
}

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.