PHP date and time addition and subtraction program code details. Today, let's take a look at the time difference between two time points in php. now we use data directly, and the strtotime and time functions are implemented. if you need them, please refer to them. Today, let's talk about how to calculate the time difference between the two time points in php. now we use data directly. the strtotime and time functions are implemented. if you need them, please refer to them.
The requirement for this example is as follows. Know a date and time,
Example: 10:10:00
I want to add 5 months based on this date and return the processed date.
Result: 10:10:00 + 5 months equals 10:10:00
This is also roughly the meaning of combining the date () and strtotime () functions of PHP functions,
The code is as follows: |
|
/** * Date addition and subtraction method in PHP * Qiongtai old house */ // Step 1, assume there is a time $ A = '2017-04-25 10:10:00 '; // Step 2, obtain the timestamp of this date $ A_time = strtotime ($ ); // Step 3: obtain the timestamp after adding five months $ B _time = strtotime ('+ 5 month', $ a_time ); // Part 4: convert the timestamp back to the date format $ B = date ('Y-m-d H: I: S', $ B _time ); Echo 'This is the date after adding five months '. $ B; // If you think the above code is too long, you can do it in one line. $ B = date ('Y-m-d H: I: S', strtotime ('+'. $ time. 'month', strtotime ($ ))); Echo 'This is the date after adding five months '. $ B; ?> |
Commonly used computing time
The code is as follows: |
|
Date_default_timezone_set ('prc'); // Default time zone Echo "today:", date ("Y-m-d", time ())," "; Echo "today:", date ("Y-m-d", strtotime ("18 Jun 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 "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 "))," "; Echo "next Thursday:", date ("Y-m-d", strtotime ("next Thursday "))," "; Echo "last Monday:". date ("Y-m-d", strtotime ("last Monday "))." "; Echo "a month ago:". date ("Y-m-d", strtotime ("last month "))." "; Echo "one month later:". date ("Y-m-d", strtotime ("+ 1 month "))." "; Echo "Ten years later:". date ("Y-m-d", strtotime ("+ 10 year "))." "; ?> |
Output result
Today: 2013-06-07
Today: 2008-06-18
Yesterday: 2013-06-06
Tomorrow: 2013-06-08
One week later: 2013-06-14
One week, two days, four hours, two seconds later: 18:18:29
Next Thursday:
Last Monday: 2013-06-03
One month ago:
One month later: 2013-07-07
Ten years later: 2023-06-07
Let's look at some date addition and subtraction functions.
The 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; } |
Bytes. I want to talk about it today...