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? Link to comment https://forums.phpfreaks.com/topic/158756-convert-date-to-string/ 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? Link to comment https://forums.phpfreaks.com/topic/158756-convert-date-to-string/#findComment-837292 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); Link to comment https://forums.phpfreaks.com/topic/158756-convert-date-to-string/#findComment-837304 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); Link to comment https://forums.phpfreaks.com/topic/158756-convert-date-to-string/#findComment-837306 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? Link to comment https://forums.phpfreaks.com/topic/158756-convert-date-to-string/#findComment-837307 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? Link to comment https://forums.phpfreaks.com/topic/158756-convert-date-to-string/#findComment-837333 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. Link to comment https://forums.phpfreaks.com/topic/158756-convert-date-to-string/#findComment-837341 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.