簡體   English   中英

strtotime PHP的MySQL的

[英]strtotime php mysql

我想要做的就是使php文件從子日期開始累積結束日期。 我不明白為什么這個strtotime函數不起作用。 我的數據庫將日期存儲為“ Ymd”。

這是代碼:

//disguised for security reasons
$db = mysql_connect("*******", "*******","********");
mysql_select_db("*******",$db);

$getad = mysql_query("SELECT id, annual_sub_date FROM members WHERE annual_sub_date!=null", $db);
while ($gad = mysql_fetch_array($getad)) {
$id = $gad['id'];
$asd = $gad['annual_sub_date'];
$aedate_time = strtotime('+1 year', $asd);
$aedate = date("Y-m-d", $aedate_time);
mysql_query("UPDATE members SET annual_end_date='$aedate', annual_active='Y' WHERE id='$id'");
}

---------解決IT ---------

我去玩了一下XBox Split / Second,然后意識到了這個問題。 我的想法回到了PHP / MySQL101。除了“!= null”部分之外,我都編寫了所有正確的代碼。

//Wrong Way
$getad = mysql_query("SELECT id, annual_sub_date FROM members WHERE annual_sub_date!=null", $db);

//Correct Way
$getad = mysql_query("SELECT id, annual_sub_date FROM members WHERE annual_sub_date IS NOT NULL", $db);

現在一切正常:)這就是您可以期望在上午5:01進行編碼的問題。

strtotime的第一個參數是字符串的絕對日期或相對日期,第二個參數是整數時間戳。 您給它一個相對日期(字符串)作為第一個參數,並給一個絕對日期(也是字符串)作為第二個參數。 您需要先使用strtotime$asd轉換$asd時間戳。

$aedate_time = strtotime('+1 year', strtotime($asd));

順便說一句,您可以使用一個查詢在SQL中進行整個日期的計算和更新,而無需花很多時間在PHP上。

這是因為strtotime需要時間戳記作為第二個參數,而不是Ymd中的字符串日期。 只需嘗試下面的代碼片段即可查看我的建議。

$gad = array('annual_sub_date' => '2010-11-21');
// wrong
// $asd = $gad['annual_sub_date'];
// good; convert to timestamp
list($year, $month, $day) = explode('-', $gad['annual_sub_date']);
$asd = mktime(0, 0, 0, $month, $day, $year);
$aedate_time = strtotime('+1 year', $asd);
$aedate = date("Y-m-d", $aedate_time);
echo $aedate . "\n";

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM