Hardbyte Posted March 24, 2006 Share Posted March 24, 2006 Hi.Currently passing data around forms and then submitting a date to the database. However the database seems to require the date format yyyy-mm-dd whereas my searches require the date dd-mm-yy. To make it simple, is there a way to change the date from dd-mm-yy to yyyy-mm-dd when pressing the search button on my form to query the database?Thanks in advance.Hardbyte Quote Link to comment Share on other sites More sharing options...
redbullmarky Posted March 24, 2006 Share Posted March 24, 2006 there's probably easier ways, but this is the way i use to convert dates to and from MySQL format:[code]function d_convert($indate,$desttype = '') { if (($indate == '00/00/0000') || ($indate == '0000-00-00')) $indate=''; // firstly return blank field if the entry param is blank if ($indate == '') return $indate; // now check which way we're converting - to or from an SQL date if ($desttype == 'SQL') { $arr = explode('/',$indate); return $arr[2].'-'.str_pad($arr[1],2,'0',STR_PAD_LEFT).'-'.str_pad($arr[0],2,'0',STR_PAD_LEFT); } else { $arr = explode('-',$indate); return $arr[2].'/'.$arr[1].'/'.$arr[0]; }}[/code]stick the above function wherever you normally keep functions, and use it like:convert normal date to SQL date ready to put into database:[code]$date = d_convert('24/03/2006', 'SQL');[/code]convert an sql date to a uk dd/mm/yy:[code]$date = d_convert('2006-03-24');[/code]been using this function myself for almost everythign i do. hope that helps Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted March 24, 2006 Share Posted March 24, 2006 You cam use a combination of the [a href=\"http://www.php.net/strtotime\" target=\"_blank\"]strtotime[/a]() and [a href=\"http://www.php.net/date\" target=\"_blank\"]date[/a]() functions:[code]<?php$search_date = '24-03-06';list($d,$m,$y) = explode('-',$search_date);echo date('Y-m-d',strtotime($m . '/' . $d . '/' $y));?>[/code]Ken Quote Link to comment Share on other sites More sharing options...
redbullmarky Posted March 24, 2006 Share Posted March 24, 2006 [!--quoteo(post=357956:date=Mar 24 2006, 04:41 PM:name=kenrbnsn)--][div class=\'quotetop\']QUOTE(kenrbnsn @ Mar 24 2006, 04:41 PM) [snapback]357956[/snapback][/div][div class=\'quotemain\'][!--quotec--]You cam use a combination of the [a href=\"http://www.php.net/strtotime\" target=\"_blank\"]strtotime[/a]() and [a href=\"http://www.php.net/date\" target=\"_blank\"]date[/a]() functions:[code]<?php$search_date = '24-03-06';list($d,$m,$y) = explode('-',$search_date);echo date('Y-m-d',strtotime($m . '/' . $d . '/' $y));?>[/code]Ken[/quote]yep. if your dates will never have dates before 1/1/1970, then the way ken suggested is much easier and better. if you need dates BEFORE then, such as date of births, etc, then you need to use the function i suggested. Quote Link to comment Share on other sites More sharing options...
Hardbyte Posted March 26, 2006 Author Share Posted March 26, 2006 Hi and thanks for everyones help.I found that the functions as mentioned above by redbullmarky are the best for what I needed.Was perfect again mate - thanks alot!Hardbyte 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.