Summary of PHP timestamp and date conversion operation examples
- 2021-11-13 01:00:17
- OfStack
This article summarizes the PHP timestamp and date conversion operations. Share it for your reference, as follows:
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 realization, let me give you an example below.
1. Time transfer function in php
strtotime(date("Y-m-d H:i"))
date("Y-m-d H:i",$unixtime)
2. Get the timestamp of today's zero point in php. To get the timestamp of zero point in unix, 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" is the converted date format,
time()
Is the timestamp to get the current time. If it is
date("Y-m-d H:i:s",time())
, it will be displayed from 1 hour to 1 minute; 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:
2018-12-18 11:38:18
date("Y-m-d",time())
After conversion, it is:
2018-12-18
Convert date to timestamp
class SaonekController extends Controller {
public function indexAction() {
/*
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. In php, the timestamp 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
strtotime("+1 day")
Current time:
echo date("Y-m-d H:i:s",time())
Results:
2018-12-18 11:43:21
Specified time:
echo date("Y-m-d H:i:s",strtotime("+1 day"))
Results:
2018-12-19 11:43:37
(2) Print the PHP timestamp at this time yesterday
strtotime("-1 day")
Results:
1545018243
Current time:
date("Y-m-d H:i",$todaytime)
0
Results:
2018-12-18 11:44:37
Specified time:
echo date("Y-m-d H:i:s",strtotime("-1 day"))
Results:
2018-12-17 11:44:55
(3) Print the timestamp for this time of the next week
strtotime("+1 week")
Current time:
date("Y-m-d H:i",$todaytime)
3
Results:
2018-12-18 11:45:22
Specified time:
date("Y-m-d H:i",$todaytime)
4
Results:
2018-12-25 11:45:36
(4) Print the timestamp at this time of last week
date("Y-m-d H:i",$todaytime)
5
Current time:
date("Y-m-d H:i",$todaytime)
6
Results:
2018-12-18 11:46:02
Specified time:
echo date("Y-m-d H:i:s",strtotime("-1 week"))
Results:
2018-12-11 11:46:16
(5) Print an PHP timestamp specifying the next day of the week
strtotime("next Thursday")
Current time:
date("Y-m-d H:i",$todaytime)
9
Results:
2018-12-18 11:46:38
Specified time:
echo date("Y-m-d H:i:s",strtotime("next Thursday"))
Results:
2018-12-20 00:00:00
(6) Print a timestamp specifying the day of the week
strtotime("last Thursday")
Current time:
echo date("Y-m-d H:i:s",time())
Results:
2018-12-18 11:46:38
Specified time:
echo date("Y-m-d H:i:s",strtotime("last Thursday"))
Results:
2018-12-13 00:00:00
Note: Don't forget the setting of time zone when using timestamp and date date setting:
date_default_timezone_set('PRC'); // Set China Time Zone
PS: Here are some time and date related tools for your reference:
Online date/day calculator:
http://tools.ofstack.com/jisuanqi/date_jisuanqi
Online date calculator/difference days calculator:
http://tools.ofstack.com/jisuanqi/datecalc
Online Date-Day Difference Calculator:
http://tools.ofstack.com/jisuanqi/onlinedatejsq
Unix timestamp (timestamp) conversion tool:
http://tools.ofstack.com/code/unixtime
More readers interested in PHP can check the topic of this site: "php Date and Time Usage Summary", "PHP Array (Array) Operation Skills Encyclopedia", "PHP Basic Syntax Introduction Course", "PHP Operation and Operator Usage Summary", "php Object-Oriented Programming Introduction Course", "PHP Network Programming Skills Summary", "php String (string) Usage Summary", "php+mysql Database Operation Introduction Course" and "php Common Database Operation Skills Summary"
I hope this article is helpful to everyone's PHP programming.