Almost all programmers engaged in program development encounter time processing problems, and PHP development is the same. Fortunately, PHP provides a lot of date and time functions. As long as you use these functions frequently and in combination, practice makes perfect in date and time processing.
The requirements for the example we are going to talk about today are as follows. Get a certain date and time,
For example: 2012-04-25 10:10:00
I want to add 5 months to this date and time and return the processed date
Result: 2012-04-25 10:10:00 plus 5 months equals 2012-09-25 10:10:00
This requirement seems simple, but it is still a bit tricky because PHP does not directly provide the addition and subtraction of date and time in the format of yyyy-mm-dd hh:ii:ss, so it can only be achieved through timestamps. Timestamps are the standard format for program conversions and are accurate to seconds. PHP can convert a variety of date formats into timestamps, and can convert timestamps back into various date formats. Combining these two features, we roughly implement three steps. First, convert the original time into a timestamp, and then add and subtract it. , and finally convert back to date format.
Of course, this is the implementation principle. Combining the PHP functions date() and strtotime() can achieve roughly the same meaning. Please see the example code
Don’t think PHP is so stupid, use time() for the current time, and find the most direct method according to your needs.
$time=time();
for($i=1;$i<=5;$i++){
$s=getdate($time-$i*24*3600);
$day[$i][mdy]=$s[mday];
$day[$i][mon]=$s[mon];
}
This way you get the past 5 If the month and day of the day are determined, replace - with + to get the month and day of the next 5 days.
However, I find that you don’t quite understand the definition of timestamp.
The next five days are time()+5*24*3600,
The past five days are time()-5*24*3600,
Why can’t we use timestamp?
It is recommended to use javascript language to write code. It is more troublesome to use PHP to complete this.
You can search online for specific countdown codes. There are many similar codes.