Skip to content

Instantly share code, notes, and snippets.

@azhai
Last active December 20, 2015 01:48
Show Gist options
  • Save azhai/6051328 to your computer and use it in GitHub Desktop.
Save azhai/6051328 to your computer and use it in GitHub Desktop.
找出上个月的这一天,没有这一天时使用月末
<?php
/**
* 找出上个月的这一天,没有这一天时使用月末
* (使用时间戳计算,避免判断跨年)
*/
function last_month_day($time)
{
$day = intval(date('d', $time)); //当月第几天
$time -= $day * 86400; //退回上月最后一天
$tail_day = intval(date('d', $time)); //上个月有多长
if ($day > $tail_day) {
//上个月较短,没有这几天,使用月末
} else {
$time -= ($tail_day - $day) * 86400;
}
return date('Y-m-d', $time);
}
$time = strtotime('2012-03-31');
echo date('Y-m-d', $time) . "\n";
echo last_month_day($time) . "\n";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment