PHP time operations

Source: Internet
Author: User

1. Calculate the data issue between the current time and the same time period of yesterday

This problem requires you to know the current time and the time after the current time minus 24

Method 1: Use time () and date ()

For example:

$ Time = time ();

$ Tomorrow = time () + 24*3600; // Same time tomorrow

Echo '4: '. date ('y: m: d H: I: s', $ tomorrow );

$ Yestoday = time () + 24*3600; // same time as yesterday

Echo '5: '. date ('y: m: d H: I: s', $ yestoday );

Method 2: Write the functions DateDiff () or DateSubstact () in other languages by yourself ()

For example:

Function DateDiff ($ interval, $ date1, $ date2)

{// Obtain the number of seconds between two dates

$ Timedifference = strtotime ($ date2)-strtotime ($ date1 );

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;

}

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 ;}

Usage:

Inetrval indicates the time interval string expression to be added, such as minute or day. number indicates the number of time intervals to be added. Date indicates the Date.
Interval (Time Interval string expression) can be any of the following values:
Yyyy year
Q Quarter
M Month
Y Day of year
D Day
W Weekday
Ww Week of year Week
H Hour
N Minute points
S Second seconds
The roles of w, y, and d are exactly the same, that is, add one day to the current date, add three months to q, and add seven days to ww.

We can save the above Code as the dateadd. inc file, and then run the following code:

<? Php

Include ('dateadd. inc ');

$ Temptime = time ();

Echo '1: '. strftime ("% Y-% m-% d % H: % M: % S", $ temptime );

$ Temptime = DateAdd ("h", 24, $ temptime );

Echo '2: '. strftime ("% Y-% m-% d % H: % M: % S", $ temptime );

Echo '3: '. date ('y: m: d H: I: s ');

$ Time = time ();

$ Tomorrow = time () + 24*3600;

Echo '4: '. date ('y: m: d H: I: s', $ tomorrow );

$ Yestoday = time () + 24*3600;

Echo '5: '. date ('y: m: d H: I: s', $ yestoday );

?>

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.