2013-06-20 64 views
1

我可以使用strtotime +1個月,但我該如何返回那一天23:59:59?PHP strtotime + 1個月並返回23:59

比如現在是2013年6月19日03:28如果我是加1個月那麼它將返回2013年7月19日03:28,但我需要2013年7月19日23:59

+0

顯示的代碼。它可能有幫助。 –

+0

任何人都可以通過研究技巧來發現一天,一週,一個月或一年的結束,就像我一樣?我剛剛意識到,至少爲了編程的目的,直覺上,一天之後的開始,可能是我真正需要的。 23:59是一天結束嗎?不,還剩下一整分鐘。 23:59:59怎麼樣?顯然,那裏還有一秒鐘。好的,23:59:59.999,那麼?可能很好,但它不像找到下一天的午夜那樣直截了當,那就是真正結束之前的那一天。例如'strtotime('下個月的第一天午夜')':) –

回答

4
date("Y-m-d 23:59:59", strtotime("+1 month")); 
3

雖然orangepill的回答有用的,我想這是更清晰:

<?php 

// You time as Unix timestamp 
$now = strtotime('2013-06-19 03:28'); 

$d = new DateTime(); 

// Whenever doing date calculations, timezone is important. Set it to "UTC" if you don't care. 
$d->setTimeZone(new DateTimeZone('America/Detroit')); 

// Initialize date (- but remember, DateTime can take many other forms of date input) 
$d->setTimeStamp($now); 

// Increase date by 1 month. 
$d->add(new DateInterval('P1M')); 

// Overwrite the "clock" part of the date 
$d->setTime(23, 59, 59); 

// Display the result: 
var_dump($d);