PHP获取相对时间戳的方法
在PHP开发中,经常需要获取当前日期以及相对于今天的昨天、明天等时间点的时间戳。
例如,参考时间示例为2013-06-20 11:12。
<?php 可以使用以下代码打印今天的日期:echo "今天:".date("Y-m-d").";"
"; 使用strtotime减去一天来获取昨天的日期:echo "昨天:".date("Y-m-d",strtotime("-1 day")), ";"
"; 使用strtotime加上一天来获取明天的日期:echo "明天:".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")).";"
"; strtotime()函数的核心作用是将人类可读的日期时间描述解析为Unix时间戳。
其函数原型为:int strtotime ( string time [, int now] )。
?>该函数预期接受一个包含美国英语日期格式的字符串,并尝试将其解析为Unix时间戳(即自January 1 1970 00:00:00 GMT起的秒数)。解析结果相对于now参数给出的时间,若未提供此参数,则默认使用系统当前时间。
-------------------------------------------------------------------以下是几种在PHP中获取前天和昨天日期的代码实现方式。
曾有面试中遇到此类问题,当时只记得MySQL中的date_sub(now(),'interval 1 day')用法,或者使用date('Y/m/d h:i:s',mktime(date('h'), date('i'), date('s'), date('m') , date('d')+1, date('Y')));这样的组合函数。
--------------------------------------------------------------------------------一种基础思路是先获取今天的UNIX时间戳。
然后从该时间戳中减去一天的秒数(24*60*60)或两天的秒数。
最后将计算后的UNIX时间戳格式化为易读的日期字符串。
--------------------------------------------------------------------------------具体代码引用如下:
<?php 首先设置时区:date_default_timezone_set('Asia/Shanghai');
#计算昨天
echo date("Y/m/d h:i:s",time()-24*60*60);
echo ";"
"; #计算前天
echo date("Y/m/d h:i:s",time()-2*24*60*60);
?> --------------------------------------------------------------------------------
此外还有更多方法,这里再介绍一种。
--------------------------------------------------------------------------------方法有很多种啊, 我也介绍一种吧:
直接使用strtotime处理ago语法:date("Y/m/d H:i:s", strtotime("1 days ago"));
处理两天前:date("Y/m/d H:i:s", strtotime("2 days ago"));
--------------------------------------------------------------------------------或者使用mktime函数指定前一天:date("Y/m/d H:i:s",mktime(0,0,0,date("m"),date("d")-1,date("Y")));
--------------------------------------------------------------------------------以前处理时间计算比较繁琐,学习后发现有很多简便方法,下面展示计算下个星期现在时间的代码。
同样需要先设置时区:date_default_timezone_set('Asia/Shanghai');
计算七天后的时间戳:$tmp = time()+60*60*24*7;
格式化输出:print date("m/d/Y H:i:s", $tmp);
--------------------------------------------------------------------------------再补充一种基于localtime的方法:
$time_yes=localtime(time()-24*60*60, true);
$time_b_yes=localtime(time()-2*24*60*60, true);提取昨天的日期:$yesterday=$time_yes['tm_mday'];
提取前天的日期:$the_day_before_yes=$time_b_yes['tm_mday'];
--------------------------------------------------------------------------------其中time()-86400代表昨天的时间戳。
以下为引用的内容:
//计算昨天
print date('Y-m-d' , strtotime('-1 day'));
//计算上星期
print date('Y-m-d' , strtotime('-1 week'));
//计算上个月
print date('Y-m-d' , strtotime('-1 month'));
//计算去年
print date('Y-m-d' , strtotime('-1 year'));
?>--------------------------------------------------------------------------------通过strtotime得到一个时间戳, 然后你自己格式化.
例如直接获取昨天的时间戳:strtotime('yesterday');
或者获取两天前的时间戳:strtotime('-2 day');