php时间戳转实例代码

来源:互联网 发布:魔法王座升阶数据大全 编辑:程序博客网 时间:2024/06/16 05:24
PHP获取“今天的时间戳”,再通过“今天”获取昨天和明天0点和23:59:59的时间戳。

结果:
array(3) { ["yesterday"]=> array(2) { [0]=> int(1395874800) [1]=> int(1395961199) } ["today"]=> array(2) { [0]=> int(1395961200) [1]=> int(1396047599) } ["tomorrow"]=> array(2) { [0]=> int(1396047600) [1]=> int(1396133999) } }

代码:

<?php
//php时间戳
//昨天,今天和明天的日期转换    
//($startstr 今天开始时间戳)
//返回(昨天,今天和明天)的0点和23点59分59秒
function alldaytostr($startstr) {
 $oneday_count = 3600 * 24;  //一天有多少秒
 //明天
 $tomorrow_s = $startstr + $oneday_count;    //明天开始
 $tomorrow_e = $tomorrow_s + $oneday_count - 1;  //明天结束
 //昨天 www.jbxue.com
 $yesterday_s = $startstr - $oneday_count;  //昨天开始
 $yesterday_e = $startstr - 1;   //昨天结束
 //今天结束
 $today_e = $tomorrow_s - 1;
 //昨天、今天和明天 0点和当天23点59分59秒合并成数组
 $allday_array = array('yesterday' => array($yesterday_s, $yesterday_e),
  'today' => array($startstr, $today_e),
  'tomorrow' => array($tomorrow_s, $tomorrow_e));
 return $allday_array;
}
//当天开始时间
$btime = date('Y-m-d'.'00:00:00',time());
//转换成“开始”的时间戳
$btimestr = strtotime($btime);
var_dump(alldaytostr($btimestr));
?>
0 0