Date operations are very commonly used. Below is a simple function. The main thing is to distinguish the time structures commonly used in PHP. Please take a look at the following piece of code, which involves most of the time operation functions. I believe it will be helpful.
<?php error_reporting(7); ini_set('display_errors', 1); ini_set('date.timezone','Asia/Shanghai'); $start_str = '2015-01-01 23:00:00'; $end_str = '2015-01-30 23:00:00'; for($i = 0; ; $i++){ $start = date_create($start_str); $interval_d = date_interval_create_from_date_string("{$i} days"); $ans =date_add($start,$interval_d); $s = date_format($ans,'Y-m-d H:i:s'); echo $s."\n"; if($s >= $end_str){ echo 'brk'."\n"; break; } //echo date_frormat($ans,'Y-m-d H:i:s')."\n"; } ?>
outputs all the times in two time periods, and the interval is 1 day. Among them, date_add() can be replaced by date_sub().
There is also time operation in mysql. You can refer to the following sql statement
select '2014-10-10',date_add('2015-1-1', interval 1 day);
The output is 2014-10-10 and 2015-01-02. In Mysql, time occupies 2 digits. For example, the reality of 2014-1-1 in Mysql is 2014-01-01. You should pay attention to these details (especially when comparing time). It should be used flexibly according to needs in the program.
The above has introduced PHP time operations, including aspects of the operation. I hope it will be helpful to friends who are interested in PHP tutorials.