PHP Output Time Difference Function Code

Source: Internet
Author: User

PHP Output Time Difference Function

Copy codeThe Code is 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 ("18 June 2008"), "<br> ";
Echo "yesterday:", date ("Y-m-d", strtotime ("-1 day"), "<br> ";
Echo "Tomorrow:", date ("Y-m-d", strtotime ("+ 1 day"), "<br> ";
Echo "one week later:", date ("Y-m-d", strtotime ("+ 1 week"), "<br> ";
Echo "one week, two days, 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 ("last Monday"). "<br> ";
Echo "a month ago:". date ("Y-m-d", strtotime ("last month"). "<br> ";
Echo "One month later:". date ("Y-m-d", strtotime ("+ 1 month"). "<br> ";
Echo "10 years later:". date ("Y-m-d", strtotime ("+ 10 year"). "<br> ";
?>

When learning PHP, it is often used to obtain the date of a certain period before or after the current time. Now we have collected data, and you can also expand and enrich it.
Copy codeThe Code is as follows:
// Obtain the day of the week (1-7)
Function GetWeek ($ times)
{
$ Res = date ('w', strtotime ($ times ));
If ($ res = 0)
$ Res = 7;
Return $ res;
}
// Obtain the current day's time
Function GetTime ($ times)
{
$ Res = date ('H: I ', strtotime ($ times ));
Return $ res;
}
// Obtain the time of the current month
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;
}
// Obtain the current time
Function GetCurrentDateTime ()
{
$ Res = date ("Y-m-d H: I: s", time ());
Return $ res;
}
// Obtain the time before or after the current time
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 several minutes before or after
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 several seconds
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 of several weeks before or after
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;
}
// Interval between several 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;
}
// Interval of several years before or after
Function GetDiffYears ($ year, $ type = 'l ')
{
If (! Strcmp ($ type, 'B '))
$ Res = date ("Y-m-d H: I: s", strtotime ("-$ year "));
If (! Strcmp ($ type, 'L '))
$ Res = date ("Y-m-d H: I: s", strtotime ("+ $ 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.