Jump to content


Photo

Changing Date Formats


  • Please log in to reply
4 replies to this topic

#1 Hardbyte

Hardbyte
  • Members
  • PipPipPip
  • Advanced Member
  • 30 posts
  • LocationMidlands, UK

Posted 24 March 2006 - 03:01 PM

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

#2 redbullmarky

redbullmarky
  • Staff Alumni
  • Advanced Member
  • 2,863 posts
  • LocationBedfordshire, England

Posted 24 March 2006 - 03:30 PM

there's probably easier ways, but this is the way i use to convert dates to and from MySQL format:

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];
   }
}

stick the above function wherever you normally keep functions, and use it like:

convert normal date to SQL date ready to put into database:
$date = d_convert('24/03/2006', 'SQL');

convert an sql date to a uk dd/mm/yy:
$date = d_convert('2006-03-24');

been using this function myself for almost everythign i do. hope that helps
"you have to keep pissing in the wind to learn how to keep your shoes dry..."

I say old chap, that is rather amusing!

#3 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 24 March 2006 - 04:41 PM

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:
<?php
$search_date = '24-03-06';
list($d,$m,$y) = explode('-',$search_date);
echo date('Y-m-d',strtotime($m . '/' . $d . '/' $y));
?>

Ken

#4 redbullmarky

redbullmarky
  • Staff Alumni
  • Advanced Member
  • 2,863 posts
  • LocationBedfordshire, England

Posted 24 March 2006 - 05:02 PM

[!--quoteo(post=357956:date=Mar 24 2006, 04:41 PM:name=kenrbnsn)--][div class=\'quotetop\']QUOTE(kenrbnsn @ Mar 24 2006, 04:41 PM) View Post[/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:
<?php
$search_date = '24-03-06';
list($d,$m,$y) = explode('-',$search_date);
echo date('Y-m-d',strtotime($m . '/' . $d . '/' $y));
?>

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.
"you have to keep pissing in the wind to learn how to keep your shoes dry..."

I say old chap, that is rather amusing!

#5 Hardbyte

Hardbyte
  • Members
  • PipPipPip
  • Advanced Member
  • 30 posts
  • LocationMidlands, UK

Posted 26 March 2006 - 02:17 PM

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




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users