試試這個功能
編輯:首先,我們將轉換兩位數年份在4位數然後我們。將形成完整的日期,並把它傳遞給函數
$original_date = '22-10-49';
$date_part = explode('-',$original_date);
$baseyear = 1900; // range is 1900-2062
$shortyear = $date_part[2];
$year = 100 + $baseyear + ($shortyear - $baseyear) % 100;
$subdate = substr($original_date, 0, strrpos($original_date, '-'));
$string = $subdate."-".$year;
echo safe_strtotime($string);
function safe_strtotime($string)
{
if(!preg_match("/\d{4}/", $string, $match)) return null; //year must be in YYYY form
$year = intval($match[0]);//converting the year to integer
if($year >= 1970) return date("Y-m-d", strtotime($string));//the year is after 1970 - no problems even for Windows
if(stristr(PHP_OS, "WIN") && !stristr(PHP_OS, "DARWIN")) //OS seems to be Windows, not Unix nor Mac
{
$diff = 1975 - $year;//calculating the difference between 1975 and the year
$new_year = $year + $diff;//year + diff = new_year will be for sure > 1970
$new_date = date("Y-m-d", strtotime(str_replace($year, $new_year, $string)));//replacing the year with the new_year, try strtotime, rendering the date
return str_replace($new_year, $year, $new_date);//returning the date with the correct year
}
return date("Y-m-d", strtotime($string));//do normal strtotime
}
輸出:1949年10月22日
來源:Using strtotime for dates before 1970
@Strawberry我沒有得到它,你是什麼意思? –
@Strawberry 2015-10-22。 1970年前沒有任何工作,日期假定在1970-2069範圍內 –
假設你有一個像22-10-15這樣的值。你會期望實現1915年或2015年?如果答案是'我期望它是2015',那麼哪一年是截止?難道只有超過'15'的任何值都指的是20世紀的一年 – Strawberry