Detailed explanation of php timestamp conversion code
- 2021-12-13 07:46:08
- OfStack
In php, we can directly use date function to convert timestamp into date. If we want to convert date into timestamp, we can use strtotime () function to realize it. Let me give you an example below.
1. Time transfer function in php
strtotime
(date())
date("Y-m-d H:i",$unixtime)
2. Obtain the timestamp of today's zero point in php to obtain the timestamp of unix of zero point, you can use
$todaytime=strtotime( " today " )
And then use the
date("Y-m-d H:i",$todaytime)
Convert to a date.
Convert timestamp to date
Timestamp conversion function:
date ("Y-m-d H: i: s", time ()), "Y-m-d H: i: s" are converted date formats, and time () is the timestamp to obtain the current time. If it is date ("Y-m-d H: i: s", time ()), it will be displayed every hour, minute and second; If it is
date ("Y-m-d", time ()), showing only the year, month and day. For example:
date("Y-m-d H:i:s",time())
After conversion, it is:
2010-07-18 18:42:48
date("Y-m-d",time())
After conversion, it is:
2010-07-18 date converted to timestamp.
class SaonekController extends Controller {
public function index
Action
(){
/*
It goes without saying that the timestamp is converted into a date
But if the date is to be converted into a timestamp, it will be used
strtotime() */
$time = time();//
Time stamp
$nowtime = date('Y-m-d H:i:s',$time);//
Generate formatted dates
$oldtime = '2010-11-10 22:19:21';
$catime = strtotime($oldtime);//
Convert date to timestamp
$nowtimes = date('Y-m-d H:i:s',$catime);//
The timestamp is back to the date
echo $nowtimes; } } ?>
3. The timestamp in php is converted to date, and different contents are displayed according to time, such as just now, minutes ago, hours ago, today, yesterday, etc.
/* Time transfer function */function transTime($ustime) {
$ytime = date("Y-m-d H:i",$ustime);
$rtime = date("n Month j Day H:i",$ustime);
$htime = date("H:i",$ustime);
$time = time() - $ustime;
$todaytime = strtotime("today");
$time1 = time() - $todaytime;
if($time < 60){
$str = ' Just now ';
}else if($time < 60 * 60){
$min = floor($time/60);
$str = $min.' Minutes ago ';
}else if($time < $time1){
$str = ' Today '.$htime;
}else{
$str = $rtime;
}
return $str;
}
Other references
Use date to convert the current timestamp and the specified timestamp into system time
(1) Print a timestamp for this time tomorrow
$todaytime=strtotime( " today " )
0
Current time:
$todaytime=strtotime( " today " )
1
Results:
$todaytime=strtotime( " today " )
2
Specified time:
echo date( " Y-m-d H:i:s " ,strtotime( " +1 day " ))
Results:
$todaytime=strtotime( " today " )
4
(2) Print the
$todaytime=strtotime( " today " )
5
(3) Print the timestamp for this time of the next week
$todaytime=strtotime( " today " )
6
(4) Print the timestamp at this time of last week
$todaytime=strtotime( " today " )
7
(5) Print an PHP timestamp specifying the next day of the week
strtotime( " next Thursday " )
Current time: echo date( " Y-m-d H:i:s " ,time())
Results: 2009-01-22 09:40:25
Specified time: echo date( " Y-m-d H:i:s " ,strtotime( " next Thursday " ))
Results: 2009-01-29 00:00:00
(6) Print a timestamp specifying the day of the week
$todaytime=strtotime( " today " )
9
The above is the details of php timestamp conversion, please pay more attention to other related articles of php Chinese website!