socross
asked on
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;
}
}
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;
}
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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.
SELECT date_format(`mydatefield`,
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.
ASKER
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-
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-
Here are some guidelines to choosing the correct answer.
https://www.experts-exchange.com/help.jsp#hi104
https://www.experts-exchange.com/help.jsp#hi69
https://www.experts-exchange.com/help.jsp#hi75
https://www.experts-exchange.com/help.jsp#hi104
https://www.experts-exchange.com/help.jsp#hi69
https://www.experts-exchange.com/help.jsp#hi75
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.
$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
}