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

Link to comment
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?

 

So, can you post how you retrieve $mydate?

Link to comment
Share on other sites

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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.