Jump to content

Changing Date Formats


Hardbyte

Recommended Posts

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
Link to comment
https://forums.phpfreaks.com/topic/5686-changing-date-formats/
Share on other sites

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
Link to comment
https://forums.phpfreaks.com/topic/5686-changing-date-formats/#findComment-20282
Share on other sites

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
Link to comment
https://forums.phpfreaks.com/topic/5686-changing-date-formats/#findComment-20292
Share on other sites

[!--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.
Link to comment
https://forums.phpfreaks.com/topic/5686-changing-date-formats/#findComment-20300
Share on other sites

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.