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 | ||||
Result:2013-04-05 03:15:02 |
代码如下 | 复制代码 |
|
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
|
2. Get the timestamp 10 days later:
代码如下 | 复制代码 |
1 echo date('Y-m-d H:i:s',strtotime("+10 days")+8*60*60); |
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); |
代码如下 | 复制代码 |
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 |
The code is as follows | Copy code |
strtotime("+7 days"); |
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 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 |
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); |
The strtotime function compares the size of two times
1): Define two fixed times;
2): Convert fixed time into timestamp through strtotime() function;
代码如下 | 复制代码 |
$time="2012年11月23日15时50时20秒"; |
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{ |