Jump to content

convert date to string


orange08

Recommended Posts

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

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);

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?

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.