2010-05-20 93 views
9

我在mysql中有一個文本列,它存儲格式爲yyyy-mm-dd的日期值。 現在,在我的php頁面中,我使用此代碼來解析日期值。在1970之前使用strtotime日期

date("F j, Y", strtotime($row['value'])); 

現在,我剛剛讀到strtotime()僅在1970年1月1日後解析值。在該日期之前,我有很多日期值。有沒有解決辦法?我不想改變我的數據庫結構。

回答

1

您應該使用date列,而不是文本之一。
date_format() SQL函數格式日期在查詢

+0

我不能改變列類型。我在文本列中有幾個值,其中一些是日期(格式爲yyyy-mm-dd)。還有另一列指定該行是否包含日期值或文本值。 – Ctroy 2010-05-20 05:57:27

+1

在這種情況下,您需要重新設計您的數據庫。 – Jacco 2010-05-20 06:15:24

+0

你是說沒有解決這個問題,沒有重新設計數據庫?我只想將格式爲yyyy-mm-dd的文本轉換爲日期格式F j,Y. 例如:1820-05-20到1820年5月20日。 – Ctroy 2010-05-20 06:31:49

13

documentation for strtotime()

的strtotime()具有周五之間的範圍的限制,1901年12月13日20時45分54秒GMT和星期二1月19日2038 03:14:07 GMT;儘管在PHP 5.1.0之前,在一些操作系統(Windows)上,這個範圍從01-01-1970到19-01-2038是有限的。

您運行的是哪個版本的PHP?並在什麼平臺上?也許是升級的時候了。

如果您使用1901年12月13日至2038年1月19日範圍之外的日期,請考慮使用PHP的DateTime對象,這些對象可以使用更廣泛的日期範圍。

程序:

$date = date_create($row['value']); 
if (!$date) { 
    $e = date_get_last_errors(); 
    foreach ($e['errors'] as $error) { 
     echo "$error\n"; 
    } 
    exit(1); 
} 

echo date_format($date, "F j, Y"); 

OOP:

try { 
    $date = new DateTime($row['value']); 
} catch (Exception $e) { 
    echo $e->getMessage(); 
    exit(1); 
} 

echo $date->format("F j, Y"); 
+0

謝謝 - 我使用Apache Solr搜索引擎,所以使用mysql date_format函數不是一個選項,因爲所有日期都需要在YYYY-MM-DDTHH-MM中編入索引-SSZ格式。這必須使用PHP進行解析(我使用PHP api來檢索搜索結果),DateTime就是票據。 編輯:這可能是其他人遇到Apache Solr遇到這個問題的答案:http://wiki.apache.org/solr/DataImportHandler#DateFormatTransformer – 2011-04-06 02:57:51

+0

@Mark:我有php5.2.6,strtotime仍然顯示空白的日期在1970年以前。你在哪裏看到「在5.1.0之前......」的事情? – 2014-10-22 07:37:06

+0

@ScottChu - 時間戳是一個有符號整數;在格林威治標準時間1970-01-01 00:00:00之前使用負數值,並且自5.1.0起已在所有平臺上使用......參考文獻位於[docs](http ://www.php.net/manual/en/function.strtotime.php)page(note 2) – 2014-10-22 07:38:43

1
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 
} 
+1

strtotime返回一個時間戳,你的函數返回一個ISO 8601的日期。這是一個非常重要的區別! – Andrew 2012-08-17 12:26:02

+0

的確你是對的。不過@Ctroy所要求的是一種獲得ISO 8601日期的方法,所以我想我的不精確性可以被原諒! – Gombo 2012-09-30 12:24:44

0

我是小白,並有關於使用的strtotime的VS爆炸一些類似的問題。我跑過這個話題,並受到了Mark Ba​​ker對日期限制變化的評論的大力幫助(謝謝!)。所以我寫這個代碼只是爲了玩這個概念。也許它會幫助像我這樣的其他新手。

只需更改頂部PHP行的日期,看看下面的日期會發生什麼 - 非常有趣。 再次感謝!

<?php $dob = "1890-11-11"; ?> 
<html> 
<head> 
<style> 

.inputdiv { 
    width:200px; 
    margin:100px auto 10px auto; 
    background-color:#CCC2FC; 
    text-align:center; 
    border:1px solid transparent;} 

.spacer{ 
    width:199px; 
    margin:20px auto 20px auto;}  

</style> 
</head> 
<body> 

<div class="inputdiv"> 
    <div class="spacer"><?php echo "Raw dob: ".$dob ?></div> 
    <div class="spacer"><?php echo "Strtotime dob: ".date("m-d-Y", strtotime($dob)) ?></div> 
    <div class="spacer"><?php list ($y, $m, $d) = explode('-', $dob); 
         $dob = sprintf("%02d-%02d-%04d", $m, $d, $y); 
         echo "Explode dob: ".$dob ?></div> 
</div> 
</body> 
</html> 
1
$date = DateTime::createFromFormat('d M Y','17 Jan 1900'); 
echo $date->format('Y-m-d'); 
相關問題