• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 4229
  • Last Modified:

DATE FORMAT PHP - MYSQL

Hi

Im having loads of issues with some functions to convert dates between php and mysql format. I need dates stored as DATE in my database so i cant go down the route of storing them as a string.

I need two functions

1
format_date_mysql($date)

TO COVERT MM-DD-YYYY to YYYY-MM-DD

2.
format_date_php($date)

TO COVERT  YYYY-MM-DD to MM-DD-YYYY

Both functions need to return '' if date var is empty.

Here is my current functions, they seemed to work initially but im running into all sorts of issues now.

ANy help would be much apprciated.

Many thanks

-s-

function format_date_php($date){
      $output = '';
      
      if($date == '')
            return $output;
      else
      {
    $output = date('m-d-Y', strtotime($date));
    return $output;
      }
}

function format_date_mysql($date){
      $output = '';
      
      if($date == '')
            return $output;
      else
      {
    $output = date('Y-m-d', strtotime($date));
    return $output;
      }
}


0
socross
Asked:
socross
1 Solution
 
steelseth12Commented:
function format_date_php($date){
     $output = '';
 
  $temp = explode("-",$date);
 
  if($date) {
 
        $output  = $temp[1]."-".$temp[2]."-".$temp[0];
 
  }
 
 return $output;
}



function format_date_mysql($date){
 
  $output = '';
 
  $temp = explode("-",$date);
 
  if($date) {
 
        $output  = $temp[2]."-".$temp[0]."-".$temp[1];
 
  }
 
 return $output;
 
}
0
 
ygouthamCommented:
any date field in mysql can be retrieved in php and let us assume the following

$mydate = '2007-10-12';  //or any other field taken from mysql
$phpdate = date("M d, Y", strtotime($mydate) );  //change the M d, Y format to your choice of m-d-y

gives you the php date or in any format that you want

$phpdate = '10-12-2007';  //or any other field obtained in php by user input
$myarray = explode("-", $phpdate);
$mydate = $myarray[2] . "-" . $myarray[0] . "-" . $myarray[1];

all you have to do is loop through an if clause and find if they were to be empty to have a standard output

if($phpdate == '') {

//echo something or do something here

}
0
 
Mark GilbertSenior Performance EngineerCommented:
If you are storing the dates in mysql then format them  before bringing them into php.  This way you get the database to do the work with your data, and you use php to manipulate it (http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html).  For example:


SELECT date_format(`mydatefield`, '%Y-%m-%d') as mynewDate;

This way when you retrieve your date data from the database it doesn't matter how mysql has it formatted to begin with, you are returning a value that you can get php to work with.  There are so many date functions you can use directly from mysql simply using the date_format() function that can often take 5 or more lines of code to do in php.  Why do extra work?  

Hope this helps.
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
socrossAuthor Commented:
Im using a javascript date picker to enter the date im my forms this is why i require the date in php format, when i get a chance i will rewrite the js to work with the mysql format.

Thanks for all your advice, all very useful.

Is it fair to give the points to steelseth12 as it was the first answer and worked, or would you rather i split them between 3 for 3 good answers??

-s-
0
 
ygouthamCommented:
my answer was just a rewrite of steelseth's and would suggest that you keep me out of the points.  you need not necessarily assign points to every solution.  go with what worked for you, so that anyone who looks at this post in future has an idea of what a working solution was.
0

Featured Post

Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now