I am trying to convert a date that I take from an email. It is in this format: Wed, 02 Aug 2006 03:59:10 -0700 And I want to convert it to: 2006-08-02 This: trim(substr($date,12,4))."-".trim(substr($date,8,3))."-".trim(substr($date,5,2)) ...will get me: 2006-Aug-02. Any quick php tips to convert the "Aug" to convert to "08"? Thanks!
Use an Array: $months = array('Jan'=>1,'Feb'=>2,'Mar'=>3,'Apr'=>4,'May'=>'5', 'Jun'=>6,'Jul'=>7,'Aug'=>8,'Sep'=>9, 'Oct'=>10,'Nov'=>11,'Dec'=>12); $month_no = $months[$month_name]; PHP: As for your conversion. You can just try using the strtotime function. Followed by the date() function. Convert the time into unix timestamp using the strtotime function. Then change it into the format of your choice using the date() function. http://www.php.net/strtotime http://www.php.net/date Thomas
I would say your best bet would be doing something like: $NewDate = date("Y-m-d", strtotime($OldDate)); PHP: Now, I've never used strtotime with the timezone offset like you have there...so I'm not sure how it works. I'll test it when I get a second and see what I come up with. Edit: Okay, after the 4 second exhaustive test, it appears to work...
I think I spoke too soon. I am seeing some unpredictable results. Is this the correct usage for date_add using a variable? I am setting a paid date and a cancel date (one month later) but the dates don't seem to be getting updated. $ydate=date("Y-m-d", strtotime($date)); echo "Date = $ydate"; The echo shows the date I want but neither of the two updates below seem to update the rows in the table. I wasn't sure how I needed to use the $ydate in the date_add function. 1) update table set paydate = '".$ydate."', canceldate = date_add($ydate, INTERVAL 1 MONTH), where username = 'xxxxxx' 2) update table set paydate = '".$ydate."', canceldate = date_add('".$ydate."', INTERVAL 1 MONTH), where username = 'xxxxxx' If I use curdate() instead of $ydate, the update works, but the problem is that $ydate won't always be the current date. The variable seems to be what is causing the problem. I'm sure I'm doing it wrong. Thanks in advance for any suggestions.
Are you getting any MySQL Errors? Execute the query using something like the following: mysql_query($Query) or die(mysql_error()); PHP: And let me know if you get any errors there. I wonder if there is something wrong with how you're including the query string. Maybe make the query look something like this: $Query = "update table set paydate = '$ydate', canceldate = date_add('$ydate', INTERVAL 1 MONTH) where username = 'XXXXX'" PHP: I could be entirely off track though....it is 6 am here.....
Yes, I do check for mysql errors and it seems to be running. See below. I tried this yesterday... $stmt = 'months'; //How many month subscription, 1 or 6? $pdate=date("Y-m-d", strtotime($date)); //Date on email of PayPal payment echo "PayDate: $pdate / "; //echos OK if ($yemail == 19.95) // 1 month subscription { $t = 1; $cdate = date("Y-m-d", strtotime($date." + ".$t." ".$stmt)); //email date + 1 month $sql = "update tbluser set pmode='P', paystatus ='".$t."', paydate = '".$pdate."', canceldate = '".$cdate."', sref='".$xsubs."' where username ='".$row1['username']."'"; } else { // 6 month subscription $t = 6; $cdate = date("Y-m-d", strtotime($date." + ".$t." ".$stmt)); // email date + 6 months $sql = "update tbluser set pmode='P', paystatus ='".$t."', paydate = '".$pdate."', canceldate = '".$cdate."', sref='".$xsubs."' where username ='".$row1['username']."'"; } mysql_query($sql) or die("Not Updated.."); // Yes, the mysql seems to be working ... but no luck on an update. Thanks again! Tom
The affected rows value shouldn't contain any data, it should only contain a number of affected rows. I'm sorry, I just realized I wasn't very clear. After you run the query, run this line: echo "Number of affected rows: " . mysql_affected_rows(); PHP: That should tell you how many rows were updated, in your case it should say 1. Also, instead of executing the query, can you place it in an echo statement and then place the output here? Replace: mysql_query($sql) or die("Not Updated.."); // Yes, the mysql seems to be working PHP: with : echo $sql; PHP: and show us the output.
I just ran a test and the $sql echo looked fine, AND the updates were made correctly. That sounded strange to me so I contacted my webhosting folks and they said that my cron job has been scheduled, and running two times, starting at the same time each day. I wonder if that had something to do with the updates not working.