Home > Backend Development > PHP Tutorial > PHP time operations

PHP time operations

WBOY
Release: 2016-08-08 09:30:30
Original
1359 people have browsed it

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(&#39;display_errors&#39;, 1);
ini_set(&#39;date.timezone&#39;,&#39;Asia/Shanghai&#39;);


$start_str = &#39;2015-01-01 23:00:00&#39;;
$end_str = &#39;2015-01-30 23:00:00&#39;;

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,&#39;Y-m-d H:i:s&#39;);
	echo $s."\n";
	if($s >= $end_str){
		echo 'brk'."\n";
		break;
	}
	//echo date_frormat($ans,'Y-m-d H:i:s')."\n";
}
?>

Copy after login

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);
Copy after login

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.

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template