Last active
December 20, 2015 01:48
-
-
Save azhai/6051328 to your computer and use it in GitHub Desktop.
找出上个月的这一天,没有这一天时使用月末
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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