PHP 处理日期、时间、时间戳相关操作
时间:2024-4-30 11:12 作者:杨佳乐 分类: PHP
//时间相关
public function timeinfo(){
//获取今天0点-24点时间戳
$today = strtotime(date('Y-m-d', time()));
$todayend = $today + 24 * 60 * 60;
//获取本周第一天及最后一天
$weekfirst=strtotime(date('Y-m-d', strtotime('this week')));
$weeklast=strtotime(date('Y-m-d', strtotime('last day next week')));
//3.获取当天年份、月份、日及天数.
echo " 本月共有:".date("t")."天";
echo " 当前年份".date('Y');
echo " 当前月份".date('m');
echo " 当前几号".date('d');
//1.获取上个月第一天及最后一天.
echo date('Y-m-01', strtotime('-1 month'));
echo "<br/>";
echo date('Y-m-t', strtotime('-1 month'));
//2016-08-10这天 2个月后的日期
echo date("Y-m-d",strtotime("+2 month",strtotime("2016-08-10")));
//获取3 年、月、日 后的日期
echo date("Y-m-d",strtotime("+3 year",time()));
echo date("Y-m-d",strtotime("+3 month",time()));
echo date("Y-m-d",strtotime("+3 day",time()));
//获取今天、昨天、明天的日期
echo "今天:".date("Y-m-d")."<br>";
echo "昨天:".date("Y-m-d",strtotime("-1 day")), "<br>";
echo "明天:".date("Y-m-d",strtotime("+1 day")). "<br>";
echo "一周后:".date("Y-m-d",strtotime("+1 week")). "<br>";
echo "一周零两天四小时两秒后:".date("Y-m-d G:H:s",strtotime("+1 week 2 days 4 hours 2 seconds")). "<br>";
echo "下个星期四:".date("Y-m-d",strtotime("next Thursday")). "<br>";
echo "上个周一:".date("Y-m-d",strtotime("last Monday"))."<br>";
echo "一个月前:".date("Y-m-d",strtotime("last month"))."<br>";
echo "一个月后:".date("Y-m-d",strtotime("+1 month"))."<br>";
echo "十年后:".date("Y-m-d",strtotime("+10 year"))."<br>";
}
//获取当月第一天和最后一天
function getthemonth($date)
{
$firstday = date('Y-m-01', strtotime(date('Y-m-d')));
$lastday = date('Y-m-d', strtotime("$firstday +1 month -1 day"));
return array($firstday,$lastday);
}
//计算两个时间戳相差天数
function timediff($begin_time,$end_time)
{
if($begin_time < $end_time){
$starttime = $begin_time;
$endtime = $end_time;
}else{
$starttime = $end_time;
$endtime = $begin_time;
}
//计算天数
$timediff = $endtime-$starttime;
$days = intval($timediff/86400);
//计算小时数
$remain = $timediff%86400;
$hours = intval($remain/3600);
//计算分钟数
$remain = $remain%3600;
$mins = intval($remain/60);
//计算秒数
$secs = $remain%60;
$res = array("day" => $days,"hour" => $hours,"min" => $mins,"sec" => $secs);
return $res;
}