lindm Posted July 12, 2008 Share Posted July 12, 2008 I store a date in the format 2007-05-04 in a mysql field varchar(10). I then have a script that extracts the two last figures of the year with a decrease: function taxyear($year) { $year1=strtotime($row[$year]); $year2=date('Y', $year1); return substr($year2,2,4); } I get totally wrong years. 2007-12-31 gives for instance '70' instead of 07 and 2006-07-04 gives me '69'. When I manually change to the string in the function like this all is fine...please help. function taxyear($year) { $year1=strtotime('2004-07-04'); $year2=date('Y', $year1); return substr($year2,2,4); } Link to comment https://forums.phpfreaks.com/topic/114418-solved-php-mysql-date-problem/ Share on other sites More sharing options...
JasonLewis Posted July 12, 2008 Share Posted July 12, 2008 Why are you passing $row[$year] in the function when there is no $row in the function. function taxyear($year) { $year1=strtotime($year); $year2=date('Y', $year1); return substr($year2,2,4); } Try that out... Link to comment https://forums.phpfreaks.com/topic/114418-solved-php-mysql-date-problem/#findComment-588385 Share on other sites More sharing options...
PFMaBiSmAd Posted July 12, 2008 Share Posted July 12, 2008 Start by using a DATE data type in your table. Then you can directly use one of several mysql functions in a query to do what you want. No slow php code is necessary. Strtotime() and date() are some of the slowest php functions. The reason why your first code does not work is $row does not exist inside of your function, so $row[$year] has no value. This error would have been pointed out by php if you turn full php error reporting on. Link to comment https://forums.phpfreaks.com/topic/114418-solved-php-mysql-date-problem/#findComment-588387 Share on other sites More sharing options...
lindm Posted July 12, 2008 Author Share Posted July 12, 2008 Of course. Thanks! Link to comment https://forums.phpfreaks.com/topic/114418-solved-php-mysql-date-problem/#findComment-588398 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.