简体   繁体   English

strtotime PHP的MySQL的

[英]strtotime php mysql

All I'm trying to do is make the php file accumulate the end date from the sub date. 我想要做的就是使php文件从子日期开始累积结束日期。 I don't understand why this strtotime function isn't working. 我不明白为什么这个strtotime函数不起作用。 My database stores dates as "Ymd". 我的数据库将日期存储为“ Ymd”。

here's the code: 这是代码:

//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'");
}

---------SOLVED IT--------- ---------解决IT ---------

I went and played XBox Split/Second for a bit and then realised the issue. 我去玩了一下XBox Split / Second,然后意识到了这个问题。 My mind went back to PHP/MySQL 101. I coded everything right except the "!=null" part. 我的想法回到了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);

Now everything works :) That's the issues you can expect coding at 5:01am. 现在一切正常:)这就是您可以期望在上午5:01进行编码的问题。

The first argument to strtotime is an absolute or relative date as a string, the second argument is an integer timestamp. strtotime的第一个参数是字符串的绝对日期或相对日期,第二个参数是整数时间戳。 You're giving it a relative date (string) as the first argument and an absolute date (also string) as the second. 您给它一个相对日期(字符串)作为第一个参数,并给一个绝对日期(也是字符串)作为第二个参数。 You need to convert $asd to a timestamp using strtotime first. 您需要先使用strtotime$asd转换$asd时间戳。

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

BTW, you could do the whole date calculation and updating in SQL with a single query, no need to take the long way around through PHP. 顺便说一句,您可以使用一个查询在SQL中进行整个日期的计算和更新,而无需花很多时间在PHP上。

It's because strtotime requires timestamp as second argument and not string date in Ymd. 这是因为strtotime需要时间戳记作为第二个参数,而不是Ymd中的字符串日期。 Just try code snippet below to see what I ment. 只需尝试下面的代码片段即可查看我的建议。

$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