Forum Moderators: coopster

Message Too Old, No Replies

how to convert date

         

ploppy

10:10 pm on May 21, 2008 (gmt 0)

10+ Year Member



hello i am trying to insert a date into a mysql date field which is in the format 0000-00-00. my date variable is in the format 26-05-2008. i have tried to insert using session, but it is just blank.

$date = $_SESSION['datedue'];

and then $date in the insert. is there a way to convert it? many thanks

StoutFiles

11:06 pm on May 21, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Look up the php date function for date conversions. But I'll give you a answer for fun.

$date = '26-05-2008';

$date = $date[6].$date[7].$date[8].$date[9].$date[2].
$date[3].$date[4].$date[2].$date[0].$date[1];

//correct format

ploppy

9:32 am on May 22, 2008 (gmt 0)

10+ Year Member



hey stoutfiles. if that answer is for fun, i'd hate to see your answer if you were angry :-) but how di get date from session and insert into db? cheers

Habtom

10:23 am on May 22, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



from session

If you are trying to get the current date, date("Y-m-d") will give you the date in the format you requested above.

If you just want to change any date from this format dd-mm-yyyy to yyyy-mm-dd, you can explode the date and reverse the order.

coopster

3:54 pm on May 22, 2008 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



but how di get date from session

Just like you did before. Don't forget to start_session() before you attempt to use the session variable. Looks like you are in dd-mm-yyyy format so you can use the PHP list and explode to get the values:

list [php.net]($dd, $mm, $yyyy) = explode [php.net]('-', $_SESSION['datedue']); // Date format: 26-05-2008 
$date = "$yyyy-$mm-$dd";