Jump to content

[SOLVED] Php mysql date problem


lindm

Recommended Posts

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

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.

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.