orange08 Posted May 19, 2009 Share Posted May 19, 2009 my date is saved in database as yyyy-mm-dd. i need to retrieve it to split into 3 different variable... i do it as $mydate is retrieve from database, then $_SESSION['yr'] = substr($mydate,0,4); $_SESSION['mth'] = substr($mydate,5,2); $_SESSION['day'] = substr($mydate,8,2); but, seem not work...the session variable sometime get the result but sometime not. so, i think i need to convert $mydate into string first... however, i don't know how to do it...any function can be used, please? Quote Link to comment Share on other sites More sharing options...
Masna Posted May 19, 2009 Share Posted May 19, 2009 Can you post how you're retrieving $mydate? Quote Link to comment Share on other sites More sharing options...
thebadbad Posted May 19, 2009 Share Posted May 19, 2009 You can convert it to a string by typecasting the variable. Although I can't see how it could be anything other than a string, when you're retrieving it from the database? $mydate = (string) $mydate; $_SESSION['yr'] = substr($mydate,0,4); $_SESSION['mth'] = substr($mydate,5,2); $_SESSION['day'] = substr($mydate,8,2); Edit: And it would be easier to use explode(): list($_SESSION['yr'], $_SESSION['mth'], $_SESSION['day']) = explode('-', (string) $mydate); Quote Link to comment Share on other sites More sharing options...
jackpf Posted May 19, 2009 Share Posted May 19, 2009 You could just do this $date = (string)$date; $date = explode('-', $date); Quote Link to comment Share on other sites More sharing options...
Masna Posted May 19, 2009 Share Posted May 19, 2009 You can convert it to a string by typecasting the variable. Although I can't see how it could be anything other than a string, when you're retrieving it from the database? So, can you post how you retrieve $mydate? Quote Link to comment Share on other sites More sharing options...
orange08 Posted May 19, 2009 Author Share Posted May 19, 2009 thanks thebadbad, and jackpf... your code is worked! but, here i got another question regarding date to ask... i have a date type field in my database table...and my php code is $date=$_POST['yr'].'-'.$_POST['mth'].'-'.$_POST['day']; then, the $date, which is in string type, is insert directly into the database without convert it to date type first...will it create any problem? Quote Link to comment Share on other sites More sharing options...
Ken2k7 Posted May 19, 2009 Share Posted May 19, 2009 Well a date is pretty much a string anyways. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.