您的位置:首页 > 其它

根据指定时间获取时间

2015-06-04 17:43 204 查看
当前年份:date('Y')
当前月份:date('m')

获取指定时间的时间戳:
当前时间:time();
指定时间: strtotime('2012-03-22')
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(上个星期四此时的时间)
等等,strtotime()方法可以通过英文文本的控制Unix时间戳的显示,而得到需要的时间日期格式。

获取当前月的第一天
function getCurMonthFirstDay($date) {
return date('Y-m-01', strtotime($date));
}

获取当前月的最后一天
function getCurMonthLastDay($date) {
return date('Y-m-d', strtotime(date('Y-m-01', strtotime($date)) . ' +1 month -1 day'));
}

获取指定日期的下个月的第一天
function getNextMonthFirstDay($date) {
return date('Y-m-d', strtotime(date('Y-m-01', strtotime($date)) . ' +1 month'));
}

获取指定日期的下个月的最后一天
function getNextMonthLastDay($date) {
return date('Y-m-d', strtotime(date('Y-m-01', strtotime($date)) . ' +2 month -1 day'));
}获取指定日期的上个月的第一天
function getPrevMonthFirstDay($date) {
return date('Y-m-d', strtotime(date('Y-m-01', strtotime($date)) . ' -1 month'));
}

获取指定日期的上个月的最后一天
function getPrevMonthLastDay($date) {
return date('Y-m-d', strtotime(date('Y-m-01', strtotime($date)) . ' -1 day'));
}

当前月的第一天
$month = time();                                               //当前时间
$year_temp = strftime("%Y", $month);                           //  当前年
$month_temp = strftime("%m", $month);                          //  当前月
$hour_temp = strftime("%H", $month);                           //当前小时
$min_temp = strftime("%M", $month);                            //当前小时
$sec_temp = strftime("%S", $month);                            //当前秒
$first_day = strftime("%d %B %Y", mktime(0, 0, 0, $month_temp, 1, $year_temp));

//当前月的第一天

当前月的最后一天-------------下个月的前一天

$next_month = strtotime("+1 month");             //下个月
$year_temp = strftime("%Y", $next_month);
$month_temp = strftime("%m", $next_month);
$hour_temp = strftime("%H", $next_month);
$min_temp = strftime("%M", $next_month);
$sec_temp = strftime("%S", $next_month);
$last_day = strftime("%d %B %Y", mktime(0, 0, 0, $month_temp, 0, $year_temp));

//当前月的最后一天
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: