Reputation: 19
All I'm trying to do is make the php file accumulate the end date from the sub date. I don't understand why this strtotime function isn't working. My database stores dates as "Y-m-d".
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---------
I went and played XBox Split/Second for a bit and then realised the issue. My mind went back to PHP/MySQL 101. I coded everything right except the "!=null" part.
//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.
Upvotes: 1
Views: 2275
Reputation: 1
It's because strtotime requires timestamp as second argument and not string date in Y-m-d. 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";
Upvotes: 0
Reputation: 522597
The first argument to strtotime
is an absolute or relative date as a string, the second argument is an integer timestamp. 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.
$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.
Upvotes: 3