Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5

Thread: explode issue

  1. #1
    Senior Coder
    Join Date
    Nov 2010
    Posts
    1,376
    Thanks
    263
    Thanked 32 Times in 31 Posts

    explode issue

    I have done this before but maybe i have looked at this too long lol..

    why is this not working

    PHP Code:

    $fulldob 
    $sql_array->mem_dob;
    //result 1965-04-04  checks out ok

    $dob_array explode('-',$fulldob);

    // result Array ( [0] => 0000 [1] => 00 [2] => 00 )   :confused: 
    Last edited by durangod; 10-05-2012 at 02:16 AM.

  • #2
    Senior Coder
    Join Date
    Nov 2010
    Posts
    1,376
    Thanks
    263
    Thanked 32 Times in 31 Posts
    ok got it not sure why i have to add this but i guess i have to convert it to date first then then explode it.


    i added this

    PHP Code:
    $modifydob date("Y-m-d"$fulldob); 

  • #3
    Senior Coder
    Join Date
    Nov 2010
    Posts
    1,376
    Thanks
    263
    Thanked 32 Times in 31 Posts
    did not know this, i guess if you want to manipulate a date format in or out of the db you have to format it as date first then do whatever then when you put it back you have to mod it again in date format.


    so out of the db like this

    PHP Code:

    $fulldob 
    $sql_array->mem_dob;

    //convert to date format before explode
    $modifydob date("Y-m-d"$fulldob);

    $dobarray explode('-',$modifydob);

    // in db it is Y M D

    $dobyr $dobarray[0];
     
    $dobmo $dobarray[1]; 

    $dobday $dobarray[2]; 

    and back into db like this


    PHP Code:

    if (checkdate($lstMonth$lstDay$txtYear))
    {
    $new_timestamp mktime(6,0,0$lstMonth$lstDay$txtYear);
    $dob date("Y-m-d"$new_timestamp);


  • #4
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    Your problem is your db isn't storing a datetime object, its storing a timestamp (or converted as such during query). I know this since it appears that this works:
    PHP Code:
    $fulldob $sql_array->mem_dob;

    //convert to date format before explode
    $modifydob date("Y-m-d"$fulldob);  // <-- right here, $fulldob is an integer if it works with date() 
    date() only works with an integer timestamp. Using a Datetime datatype in SQL will format it as Y-m-d automatically, so in PHP you can dump that directly to the DateTime class in PHP:
    PHP Code:
    $dt = new DateTime($sql_array->mem_dob);
    $dobyr $dt->format('Y');
    $dobmo $dt->format('n');
    $dobmo $dt->format('j'); 

  • Users who have thanked Fou-Lu for this post:

    durangod (10-04-2012)

  • #5
    Senior Coder
    Join Date
    Nov 2010
    Posts
    1,376
    Thanks
    263
    Thanked 32 Times in 31 Posts
    Thanks it was driven me nuts, thanks for the lesson ill try to remember that.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •