nmcabe Posted September 12, 2011 Share Posted September 12, 2011 I am working on a project where I need PHP and mysql to perform calculations on dates, so I am storing them in the mysql format 2011-09-12, I am writing a custom function that will convert this into a date('M d Y') format, but all I get when I run my function is "1" This is my first custom function, so i am sure it is a simple fix, I am just at the end of my spectrum on functions. I can do this as an inline script, but will need this on various pages function readableDateConvert($mysqlTime) { $phpTime = strtotime($mysqlTime); $readableTime = date('M d Y',$newTime); return $readableTime; } Quote Link to comment https://forums.phpfreaks.com/topic/246970-date-conversion-function/ Share on other sites More sharing options...
AbraCadaver Posted September 12, 2011 Share Posted September 12, 2011 You haven't defined $newTime. Turn on error reporting and you will get a notice for that. Plus you are just formating it for the year: function readableDateConvert($mysqlTime) { return date('M d Y', strtotime($mysqlTime)); } Quote Link to comment https://forums.phpfreaks.com/topic/246970-date-conversion-function/#findComment-1268357 Share on other sites More sharing options...
nmcabe Posted September 12, 2011 Author Share Posted September 12, 2011 You haven't defined $newTime. Turn on error reporting and you will get a notice for that. Plus you are just formating it for the year: function readableDateConvert($mysqlTime) { return date('M d Y', strtotime($mysqlTime)); } While I was trying to get it to work, I changed some variable names and made this mistake, I did have it defined before I posted this, however your much shorter method of doing things than mine worked perfect, thank you so much for your time, here is my final working code function readableDateConvert($mysqlTime) { return date('M d Y', strtotime($mysqlTime)); } Quote Link to comment https://forums.phpfreaks.com/topic/246970-date-conversion-function/#findComment-1268359 Share on other sites More sharing options...
AbraCadaver Posted September 12, 2011 Share Posted September 12, 2011 Cool. If you will use different formats you might do something like this: function readableDateConvert($mysqlTime, $format='M d Y') { return date($format, strtotime($mysqlTime)); } Quote Link to comment https://forums.phpfreaks.com/topic/246970-date-conversion-function/#findComment-1268361 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.