PHP Date Reformat

Discussion in 'PHP' started by tsptom, Aug 8, 2006.

  1. #1
    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!
     
    tsptom, Aug 8, 2006 IP
  2. coderlinks

    coderlinks Peon

    Messages:
    282
    Likes Received:
    19
    Best Answers:
    0
    Trophy Points:
    0
    #2
    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
     
    coderlinks, Aug 8, 2006 IP
    tsptom likes this.
  3. ip076

    ip076 Peon

    Messages:
    79
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    0
    #3
    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...
     
    ip076, Aug 8, 2006 IP
    coderlinks likes this.
  4. Cryogenius

    Cryogenius Peon

    Messages:
    1,280
    Likes Received:
    118
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Yeah, strtotime() would indead be the best solution to this problem.
     
    Cryogenius, Aug 9, 2006 IP
  5. tsptom

    tsptom Well-Known Member

    Messages:
    257
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    138
    #5
    Thanks! - I'll see how it goes.
     
    tsptom, Aug 9, 2006 IP
  6. tsptom

    tsptom Well-Known Member

    Messages:
    257
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    138
    #6
    That worked like a charm! Thanks again everyone!
     
    tsptom, Aug 12, 2006 IP
  7. tsptom

    tsptom Well-Known Member

    Messages:
    257
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    138
    #7
    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.
     
    tsptom, Aug 16, 2006 IP
  8. ip076

    ip076 Peon

    Messages:
    79
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    0
    #8
    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.....
     
    ip076, Aug 17, 2006 IP
  9. tsptom

    tsptom Well-Known Member

    Messages:
    257
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    138
    #9
    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
     
    tsptom, Aug 17, 2006 IP
  10. ip076

    ip076 Peon

    Messages:
    79
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    0
    #10
    What is returned if you look at the affected rows information?
     
    ip076, Aug 17, 2006 IP
  11. tsptom

    tsptom Well-Known Member

    Messages:
    257
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    138
    #11
    I saw one or two nulls, but mainly the old expired dates.
     
    tsptom, Aug 17, 2006 IP
  12. ip076

    ip076 Peon

    Messages:
    79
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    0
    #12
    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.
     
    ip076, Aug 18, 2006 IP
    tsptom likes this.
  13. tsptom

    tsptom Well-Known Member

    Messages:
    257
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    138
    #13
    I'll try that. Thanks!
     
    tsptom, Aug 18, 2006 IP
  14. tsptom

    tsptom Well-Known Member

    Messages:
    257
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    138
    #14
    I just ran a test and the $sql echo looked fine, AND the updates were made correctly. :eek:

    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.
     
    tsptom, Aug 18, 2006 IP
  15. ip076

    ip076 Peon

    Messages:
    79
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    0
    #15
    Well...you know what they say...if it works...dont fix it.... :)
     
    ip076, Aug 18, 2006 IP
  16. netgene

    netgene Active Member

    Messages:
    37
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    61
    #16
    i totally Agree..
     
    netgene, Aug 18, 2006 IP