date('m/d/Y', strtotime('-1.5 years'))
退貨,用於今天(2012年6月18日),06/18/2017
。strtotime可以處理分數嗎?
這是爲什麼,有沒有什麼辦法讓strtotime處理分數年?它似乎正在改變「-1.5」到「+5」。
編輯:所以你知道,這是在PHP 5.1,所以更新的日期功能不可用。
date('m/d/Y', strtotime('-1.5 years'))
退貨,用於今天(2012年6月18日),06/18/2017
。strtotime可以處理分數嗎?
這是爲什麼,有沒有什麼辦法讓strtotime處理分數年?它似乎正在改變「-1.5」到「+5」。
編輯:所以你知道,這是在PHP 5.1,所以更新的日期功能不可用。
您的最佳解決方案可能是將其轉換爲幾個月。所以1.5 years
變成18 months
這將工作。
錯誤報告https://bugs.php.net/bug.php?id=62353&edit=3已針對此問題打開。
有一個名爲timelib的庫,它包含所有的時間函數。看起來好像將相對時間轉換爲時間戳有問題。
下面是它創建的相對時間的函數:
static void timelib_set_relative(char **ptr, timelib_sll amount, int behavior, Scanner *s)
{
const timelib_relunit* relunit;
if (!(relunit = timelib_lookup_relunit(ptr))) {
return;
}
switch (relunit->unit) {
case TIMELIB_SECOND: s->time->relative.s += amount * relunit->multiplier; break;
case TIMELIB_MINUTE: s->time->relative.i += amount * relunit->multiplier; break;
case TIMELIB_HOUR: s->time->relative.h += amount * relunit->multiplier; break;
case TIMELIB_DAY: s->time->relative.d += amount * relunit->multiplier; break;
case TIMELIB_MONTH: s->time->relative.m += amount * relunit->multiplier; break;
case TIMELIB_YEAR: s->time->relative.y += amount * relunit->multiplier; break;
case TIMELIB_WEEKDAY:
TIMELIB_HAVE_WEEKDAY_RELATIVE();
TIMELIB_UNHAVE_TIME();
s->time->relative.d += (amount > 0 ? amount - 1 : amount) * 7;
s->time->relative.weekday = relunit->multiplier;
s->time->relative.weekday_behavior = behavior;
break;
case TIMELIB_SPECIAL:
TIMELIB_HAVE_SPECIAL_RELATIVE();
TIMELIB_UNHAVE_TIME();
s->time->relative.special.type = relunit->multiplier;
s->time->relative.special.amount = amount;
}
}
注:此錯誤也導致了原來同一個問題:
echo strtotime("1.5 days ago");
進入-5天,-5小時,而不是所需的-1天和12小時(相對)。
甜!謝謝。 – Andrew
是的,我只是在想,'floor($ years * 12)'或者類似的東西。儘管如此,我很好奇,爲什麼它會崩潰? – Andrew
也正在寫'使用月'。 – Brian