How does PHP timestamp work, and how to get the time

I often encounter the need for PHP to obtain various time at work, and now I would like to make some summary. The first thing you need to know is that the method of obtaining time in php is date(), and the method of obtaining timestamp in php is time(), strtotime(). Instructions are given below. date()The format is: date($format, $timestamp)
formatis-format, timestampis-timestamp (optional).
time()Returns the Unix timestamp of the current time, with no arguments.
strtotime($time, $now)Parse any English textual datetime description into Unixa timestamp. $timeRequired, specifies the time string to be parsed; $nowthe timestamp used to calculate the return value, if this parameter is omitted, the current time will be used

The meaning of each letter in the date() function string format:

a - "am" or "pm"
A - "AM" or "PM"
d - a few days, two digits, if less than two digits will be filled with zeros; such as: "01" to "31"
D - week A few, three English letters; such as: "Fri"
F - month, full name in English; such as: "January"
h - hour in 12-hour format; such as: "01" to "12"
H - hour in 24-hour format; Such as: "00" to "23"
g - hours in 12-hour format, less than two digits will not be filled with zeros; eg: "1" to 12" G
- hours in 24-hour format, less than two digits will not be filled with zeros; eg: " 0" to "23"
i - minutes; such as: "00" to "59"
j - days, two digits, if less than two digits do not fill zero; such as: "1" to "31"
l - day of the week, Full name in English; eg: "Friday"
m - month, two digits, if less than two digits, zeros will be padded in front; eg: "01" to "12"
n - month, two digits, if less than two digits, no Zero padding; eg: "1" to "12"
M - month, three English letters; eg:"Jan"
s - second; such as: "00" to "59"
S - suffix plus English ordinal number, two English letters; such as: "th", "nd"
t - the number of days in the specified month; such as: "28" to "31"
U - total seconds
w - day of the week in numeric format, eg: "0" (Sunday) to "6" (Saturday)
Y - year, four digits; eg: "1999"
y - year, two digits; eg: "99"
z - day of the year; eg: "0" to "365"

Example of usage of strtotime($time):

echo strtotime('2012-03-22');输出结果:1332427715(此处结果为随便写的,仅作说明使用)
echo strtotime(date('Y-d-m'));输出结果:(结合date(),结果同上)(时间日期转换为时间戳)
strtotime()还有个很强大的用法,参数可加入对于数字的操作、年月日周英文字符,示例如下:
echo date('Y-m-d H:i:s',strtotime('+1 day'));输出结果:2012-03-23 23:30:33(会发现输出明天此时的时间)
echo date('Y-m-d H:i:s',strtotime('-1 day'));输出结果:2012-03-21 23:30:33(昨天此时的时间)
echo date('Y-m-d H:i:s',strtotime('+1 week'));输出结果:2012-03-29 23:30:33(下个星期此时的时间)
echo date('Y-m-d H:i:s',strtotime('next Thursday'));输出结果:2012-03-29 00:00:00(下个星期四此时的时间)
echo date('Y-m-d H:i:s',strtotime('last Thursday'));输出结果:2012-03-15 00:00:00(上个星期四此时的时间)

 Example of usage of strtotime($time):

echo "明天凌晨的时间戳:".strtotime(date('Y-m-d',strtotime('+1 day')));
echo"一周后:".date("Y-m-d",strtotime("+1 week"));
echo"一周零两天四小时两秒后:".date("Y-m-d G:H:s",strtotime("+1 week 2 days 4 hours 2 seconds"));
echo"下个星期四:".date("Y-m-d",strtotime("next Thursday"));
echo"上个周一:".date("Y-m-d",strtotime("last Monday"));
echo"一个月前:".date("Y-m-d",strtotime("last month"));
echo"一个月后:".date("Y-m-d",strtotime("+1 month"));
echo"十年后:".date("Y-m-d",strtotime("+10 year"));
//今天开始时间    $beginToday= date("Y-m-d H:i:s",mktime(0,0,0,date('m'),date('d'),date('Y')));
//今天结束时间    $endToday= date("Y-m-d H:i:s",mktime(0,0,0,date('m'),date('d')+1,date('Y'))-1);
//昨天开始时间    $beginYesterday= date("Y-m-d H:i:s",mktime(0,0,0,date('m'),date('d')-1,date('Y')));
//昨天结束时间    $endYesterday= date("Y-m-d H:i:s",mktime(0,0,0,date('m'),date('d'),date('Y'))-1);
//本周开始时间    $beginThisweek = date("Y-m-d H:i:s",mktime(0, 0 , 0,date("m"),date("d")-date("w")+1,date("Y")));
//本周结束时间    $endThisweek = date("Y-m-d H:i:s",mktime(23,59,59,date("m"),date("d")-date("w")+7,date("Y")));
//上周开始时间    $beginLastweek= date("Y-m-d H:i:s",mktime(0,0,0,date('m'),date('d')-date('w')+1-7,date('Y')));
//上周结束时间    $endLastweek= date("Y-m-d H:i:s",mktime(23,59,59,date('m'),date('d')-date('w')+7-7,date('Y')));
//本月开始时间    $beginThismonth = date("Y-m-d H:i:s",mktime(0,0,0,date('m'),1,date('Y')));
//本月结束时间    $endThismonth = date("Y-m-d H:i:s",mktime(23,59,59,date('m'),date('t'),date('Y')));
//上月开始时间    $beginLastmonth = date("Y-m-d H:i:s",mktime(0, 0 , 0,date("m")-1,1,date("Y")));
 //上月结束时间    $endLastmonth = date("Y-m-d H:i:s",mktime(23,59,59,date("m") ,0,date("Y")));
//本季度开始时间    $season = ceil((date('n'))/3);    
//当前是第几季度    $beginThisseason = date('Y-m-d H:i:s', mktime(0, 0, 0,$season*3-3+1,1,date('Y')));
//本季度结束时间    $endThisseason = date('Y-m-d H:i:s', mktime(23,59,59,$season*3,date('t',mktime(0, 0 , 0,$season*3,1,date("Y"))),date('Y')));
//上季度开始时间    $season1 = ceil((date('n'))/3)-1;
//上季度是第几季度    $beginLastseason = date('Y-m-d H:i:s', mktime(0, 0, 0,$season1*3-3+1,1,date('Y')));
//上季度结束时间    $endLastseason = date('Y-m-d H:i:s', mktime(23,59,59,$season1*3,date('t',mktime(0, 0 , 0,$season1*3,1,date("Y"))),date('Y')));
//本年开始时间    $beginThisyear=date("Y",time())."-1"."-1";
//本年结束时间    $endThisyear =date("Y",time())."-12"."-31";

Guess you like

Origin blog.csdn.net/wangxuanyang_zer/article/details/129960039