Home > Backend Development > PHP Tutorial > Usage of strtotime function in PHP_PHP tutorial

Usage of strtotime function in PHP_PHP tutorial

WBOY
Release: 2016-07-20 10:59:01
Original
854 people have browsed it

The strtotime function in PHP can convert logs into timestamps. It can easily allow us to calculate the date and time minutes and seconds. Now I will introduce to you the usage of the strtotime function. Students in need can refer to it. ​

1. Get the timestamp of the current time!

a. Use strtotime('now'); to get the current timestamp! (Because there is a difference of 8 hours between the current PHP time and the real time)

The code is as follows Copy code
 代码如下 复制代码

1 echo date('Y-m-d H:i:s',strtotime('now')+8*60*60);
结果:2013-04-05 03:15:02

1 echo date('Y-m-d H:i:s',strtotime('now')+8*60*60);

Result:2013-04-05 03:15:02

 代码如下 复制代码


1 echo date('Y-m-d H:i:s',time()+8*60*60);
结果:2013-04-05 03:15:02

b. You can also use time() to get it directly!

The code is as follows Copy code

1 echo date('Y-m-d H:i:s',time()+8*60*60);

Result:2013-04-05 03:15:02
 代码如下 复制代码
1 strtotime("+10 days")+8*60*60

2. Get the timestamp 10 days later:

 代码如下 复制代码

1 echo date('Y-m-d H:i:s',strtotime("+10 days")+8*60*60);

How to use:

Output the date 10 days later for easy viewing.

The code is as follows Copy code

1 echo date('Y-m-d H:i:s',strtotime("+10 days")+8*60*60);

Result:2013-04-15 03:15:02
 代码如下 复制代码

1 strtotime("+1 week")+8*60*60

3. Get the timestamp of a week:

 代码如下 复制代码

strtotime("+7 days");

How to use:

The code is as follows Copy code

1 strtotime("+1 week")+8*60*60

 代码如下 复制代码

1 strtotime ("+1 week 2 days 4 hours 2 seconds")+8*60*60
1 echo date('Y-m-d H:i:s',strtotime ("+1 week 2 days 4 hours 2 seconds")+8*60*60);

Or you can use the above method to get the timestamp by day. Written like:
The code is as follows Copy code
strtotime("+7 days");
4. You can also get the timestamp by specifying weeks, days, hours and seconds Usage:
The code is as follows Copy code
1 strtotime ("+1 week 2 days 4 hours 2 seconds")+8*60*60 1 echo date('Y-m-d H:i:s',strtotime ("+1 week 2 days 4 hours 2 seconds")+8*60*60);

Result:2013-04-12 07:15:04;

5. Get the timestamp of last Monday or next Thursday:

How to use:

a. Get the timestamp of last Monday:

The code is as follows Copy code
 代码如下 复制代码

1 strtotime ("last Monday")+8*60*60
1 echo date('Y-m-d H:i:s',strtotime ("last Monday")+8*60*60);

1 strtotime ("last Monday")+8*60*60

1 echo date('Y-m-d H:i:s',strtotime ("last Monday")+8*60*60);

Result:2013-04-01 08:00:00;

 代码如下 复制代码

1 strtotime ("next Thursday")+8*60*60
1 echo date('Y-m-d H:i:s',strtotime ("next Thursday")+8*60*60);

b. Get the timestamp of next Thursday

The code is as follows Copy code
1 strtotime ("next Thursday")+8*60*60

1 echo date('Y-m-d H:i:s',strtotime ("next Thursday")+8*60*60);

Result:2013-04-11 08:00:00;


The strtotime function compares the size of two times

The strtotime() function compares two fixed times, as follows:

1): Define two fixed times;

2): Convert fixed time into timestamp through strtotime() function;

 代码如下 复制代码

$time="2012年11月23日15时50时20秒";
$times="2013-01-14 09:09:09";
if(strtotime($time)-strtotime($times)<0){
echo "时间:".$time." 早于时间:".$times;
}else{
echo "时间:".$times." 早于时间:".$time;
}
echo "
它们相差的时间值是:".(strtotime($time)-strtotime($times))
?>

3): Compare the values ​​of two timestamps.

The example code is as follows:

The code is as follows Copy code

$time="2012-11-23 15:50:20"; $times="2013-01-14 09:09:09";
if(strtotime($time)-strtotime($times)<0){

echo "Time: ".$time." is earlier than time: ".$times;

}else{

echo "Time: ".$times." is earlier than time: ".$time; echo " The time value difference between them is: ".(strtotime($time)-strtotime($times)) ?>
The above code runs and the results are as follows: Time: 15:50:20 on November 23, 2012. Earlier than time: 2013-01-14 09:09:09 The time value between them is: -1358125749 http://www.bkjia.com/PHPjc/445625.html
www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/445625.htmlTechArticleThe strtotime function in php can convert logs into timestamps. It can easily allow us to divide date and time For calculation of seconds, let me introduce to you how to use the strtotime function. If necessary...
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