由于php内置时间函数 strtotime 在求上个月这个功能上存在bug,所以放弃不用了……
上个自己写的临时用的,楼主看看:
$thismonth = date('m');
$thisyear = date('Y');
if($thismonth==1) {
$lastmonth = 12;
$lastyear = $thisyear-1;
} else {
$lastmonth = $thismonth - 1;
$lastyear = $thisyear;
}
$lastStartDay = $lastyear'-'$lastmonth'-1';
$lastEndDay = $lastyear'-'$lastmonth'-'date('t',strtotime($lastStartDay));
echo 'lastStartDay = '$lastStartDay;
echo '<br/>';
echo 'lastEndDay = '$lastEndDay;
PHP获取开始和结束时间
//当前时间
$start
=
strtotime(date('Y-m-d
H:i:s'));
//时长,时间长度(秒为单位,例子中为120秒,2分钟后,实际时间可自行修改或程序计算得出)
//如果是1周后,则为$start
+
(7
24
60
60);
$long
=
$start
+
120
//结束时间
$end
=
date('Y-m-d
H:i:s',
$long);
php可以用函数time()来获取Unix
时间戳,但是只能获取当前的,不能填入参数计算
php获取指定月份天数, php根据日期获取该月天数。今天给大家推荐三种方法,直接上代码:
方法一:
$days = date("t");
echo "当前月的天数 "$days"<br/>";
方法二:
$days = date('t', strtotime("2030-12"));
echo "返回2030年12月的天数 "$days"<br/>";
另外还有一种方法是使用函数直接实现,可参考:>
我把我写的分享给你吧
/显示日历
@param int $time 时间戳
/
private function __calendarPanel($time = null) {
$time || $time = time();
$dateinfo = getdate($time);
$calendar = array(
'year' => $dateinfo['year'],
'month' => $dateinfo['mon'],
'day' => $dateinfo['mday'],
);
$m_start = strtotime(date('Y-m-01', $time)); //本月第一天
$m_start_w = get_week($m_start, true); //本月第一天星期索引,0表示星期日
$m_end = strtotime('+1 month', $m_start) - 86400; //本月最后一天
$m_end_w = get_week($m_end, true); //本月最后一天星期索引,0表示星期日
//补齐上月日期
for ($i = 0; $i < $m_start_w; $i++) {
$calendar['days'][] = array(
'style' => 'bef_month',
'day' => abs(date('d', $m_start - ($m_start_w - $i) 86400)),
);
}
//本月日期
for ($i = $m_start; $i <= $m_end; $i += 86400) {
$calendar['days'][] = array(
'style' => 'the_month' (date('d', $i) == $calendar['day'] " bold" : ""),
'day' => abs(date('d', $i)),
);
}
//补齐下月日期
for ($i = $m_end_w + 1; $i <= 6; $i ++) {
$calendar['days'][] = array(
'style' => 'aft_month',
'day' => abs(date('d', $m_end + ($i - $m_end_w) 86400)),
);
}
return $calendar;
}
日历都存到返回的一个数组里了,你打印的时候,一行放7列,第一列星期日
使用time()函数先返回当前的时间戳秒数,然后+上本月剩余的x天246060
echo date("Y年-m月-d日-N,H点:i分:s秒",time()+(x246060));
一、使用函式 date() 实现
在编辑器中输入<php echo $showtime=date("Y-m-d H:i:s");>,点击回车就可以得知当前的时间。其中Y是代表4位的年份,H是24小时制,i 是分钟,如: "00" 至 "59" 。s -是秒,如: "00" 至 "59" 。
d 是几日,二位数字,若不足二位则前面补零。 如: "01" 至 "31" 。m代表月份,二位数字,若不足二位则在前面补零,如: "01" 至 "12" 。
二、使用time函数
在编辑器中输入echo date("y-m-d",$time)点击回车就可以得知当前的时间,其中Y是代表4位的年份,m代表月份,二位数字,若不足二位则在前面补零,如: "01" 至 "12" 。d 是几日,二位数字,若不足二位则前面补零。 如: "01" 至 "31" 。
三、使用strftime函数
在编辑器中输入echo strftime ("%hh%m %a %d %b" ,time());点击回车就可以得知当前的时间。
扩展资料:
Date/Time 函数
一、time — 返回当前的 Unix 时间戳
二、timezone_abbreviations_list — 别名 DateTimeZone::listAbbreviations
三、timezone_identifiers_list — 别名 DateTimeZone::listIdentifiers
四、timezone_location_get — 别名 DateTimeZone::getLocation
五、date — 格式化一个本地时间/日期
六、getdate — 取得日期/时间信息
七、gettimeofday — 取得当前时间
八、gmdate — 格式化一个 GMT/UTC 日期/时间
九、gmmktime — 取得 GMT 日期的 UNIX 时间戳
参考资料:
百度百科——PHP
以上就是关于php如何求上一个月月初至月末全部的内容,包括:php如何求上一个月月初至月末、PHP怎么获得一天,一周,一个月的起始和结束的时间戳求高人指点、php 如何用date取得指定月份有多少天等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)